Required snippets for android workshop
https://demo1738991.mockable.io/libraryRooms@Override
protected JSONObject doInBackground(Context... contexts) {
// this will be required to access string resource weather_url
Context context = contexts[0];
// this is url address from which we need data
String urlString = context.getResources().getString(R.string.weather_url);
// this is URL object which can open connection for us
URL url = null;
// this is the connection object opened by URL object
HttpsURLConnection httpsURLConnection = null;
// we want our json data from api to store it as JSON Object
JSONObject jsonObject = null;
// from established connection, this can bring us the data
InputStream inputStream = null;
try{
// let's make URL object using url address
url = new URL(urlString);
// once we have URL object, we can open the connection.
// make sure to have Network access permissions
httpsURLConnection = (HttpsURLConnection) url.openConnection();
// REST request method (GET, POST, PUT....)
httpsURLConnection.setRequestMethod("GET");
// Now our httpURLConnection is ready so let's connect it to get data
httpsURLConnection.connect();
// input stream to read data from connection
inputStream = httpsURLConnection.getInputStream();
// buffer reader to temp store incoming data
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//make full response by appending individual lines
String temp, response = "";
while ((temp = bufferedReader.readLine()) != null) {
response += temp;
}
//from this response, make a JsonObject
jsonObject = (JSONObject) new JSONTokener(response).nextValue();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (inputStream != null) {
try {
// this will close inputStream and bufferReader as well
inputStream.close();
} catch (IOException ignored) {
ignored.printStackTrace();
}
}
if (httpsURLConnection != null) {
// disconnect the connection
httpsURLConnection.disconnect();
}
}
return jsonObject;
}https://demo1738991.mockable.io/onlineweather