Last active
February 8, 2018 02:39
-
-
Save hbb20/e4d87128371632d7a9bf79ba42ce04c1 to your computer and use it in GitHub Desktop.
Required snippets for android workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dummy data | |
weatherDataArray = new String[]{"Today, February 7 - Clear - 17°C / 15°C", | |
"Tomorrow - Cloudy - 19°C / 15°C", | |
"Friday - Thunderstorms - 21°C / 9°C", | |
"Saturday - Thunderstorms - 16°C / 7°C", | |
"Sunday - Rainy - 16°C / 8°C", | |
"Monday - Partly Cloudy - 15°C / 10°C", | |
"Tuesday, February 13 - Meatballs - 16°C / 18°C", | |
"Wednesday, February 14 - Cloudy - 19°C / 15°C", | |
"Thursday, February 15 - Stormy - 30°C / 11°C", | |
"Friday, February 16 - Hurricane - 21°C / 9°C", | |
"Saturday, February 17 - Meteors - 16°C / 7°C", | |
"Sunday, February 18 - Apocalypse - 16°C / 8°C", | |
"Monday, February 19 - Post Apocalypse - 15°C / 10°C"}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://demo1738991.mockable.io/onlineweather |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://demo1738991.mockable.io/libraryRooms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment