Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. public class WeatherUtils {
  2.  
  3.  
  4. private static final String LOG_TAG = WeatherUtils.class.getSimpleName();
  5.  
  6.  
  7. public WeatherUtils() {
  8.  
  9. }
  10.  
  11. public static List<Weather> fetchNewsData(String requestUrl) throws JSONException {
  12. URL url = createUrl(requestUrl);
  13. String jsonResponse = null;
  14. try {
  15. jsonResponse = makeHttpsRequest(url);
  16. } catch (IOException e) {
  17. Log.e(LOG_TAG, "Problem making the HTTP request.", e);
  18. }
  19. List<Weather> weather = extractFromJSONResponse(jsonResponse);
  20.  
  21. return weather;
  22. }
  23.  
  24. private static URL createUrl(String stringUrl) {
  25.  
  26. URL url = null;
  27. try {
  28. url = new URL(stringUrl);
  29. } catch (MalformedURLException e) {
  30. Log.e(LOG_TAG, "Error with creating URL", e);
  31. }
  32.  
  33. return url;
  34. }
  35.  
  36. private static String makeHttpsRequest(URL url) throws IOException {
  37. String jsonResponse = "";
  38. if (url == null) {
  39.  
  40. return jsonResponse;
  41. }
  42.  
  43. HttpURLConnection urlConnection = null;
  44. InputStream inputStream = null;
  45. try {
  46. urlConnection = (HttpURLConnection) url.openConnection();
  47. urlConnection.setReadTimeout(10000 /*milliseconds*/);
  48. urlConnection.setConnectTimeout(15000 /*milliseconds*/);
  49. urlConnection.setRequestMethod("GET");
  50. urlConnection.connect();
  51.  
  52. if (urlConnection.getResponseCode() == 200) {
  53. inputStream = urlConnection.getInputStream();
  54. jsonResponse = readFromStream(inputStream);
  55.  
  56. } else {
  57. Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
  58. }
  59. } catch (IOException e) {
  60. Log.e(LOG_TAG, "Problem retrieving Book JSON results.", e);
  61.  
  62. } finally {
  63. if (urlConnection != null) {
  64. urlConnection.disconnect();
  65. }
  66. if (inputStream != null) {
  67. inputStream.close();
  68. }
  69. }
  70.  
  71. return jsonResponse;
  72. }
  73.  
  74. private static String readFromStream(InputStream inputStream) throws IOException {
  75.  
  76. StringBuilder output = new StringBuilder();
  77. if (inputStream != null) {
  78. InputStreamReader inputStreamReader;
  79. inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
  80. BufferedReader reader = new BufferedReader(inputStreamReader);
  81. String line = reader.readLine();
  82. while (line != null) {
  83. output.append(line);
  84. line = reader.readLine();
  85. }
  86. }
  87.  
  88. return output.toString();
  89. }
  90.  
  91. private static List<Weather> extractFromJSONResponse(String JSONResponse) throws JSONException {
  92. if (TextUtils.isEmpty(JSONResponse)) {
  93. return null;
  94. }
  95.  
  96. List<Weather> weather = new ArrayList<>();
  97.  
  98.  
  99. try {
  100.  
  101. JSONObject jsonResponse = new JSONObject(JSONResponse);
  102. JSONObject forecast = jsonResponse.getJSONObject("forecast");
  103. JSONObject simpleForecast = forecast.getJSONObject("simpleforecast");
  104. JSONArray listArray = simpleForecast.getJSONArray("forecastday");
  105. for (int i = 0; i < listArray.length(); i++) {
  106.  
  107. JSONObject currentWeather = listArray.getJSONObject(i);
  108.  
  109. String iconUrl = currentWeather.getString("icon_url");
  110.  
  111. double humidity = currentWeather.getDouble("avehumidity");
  112.  
  113. JSONObject highTempObject = currentWeather.getJSONObject("high");
  114. String maxTemp = highTempObject.getString("celsius");
  115.  
  116. JSONObject lowTempObject = currentWeather.getJSONObject("low");
  117. String minTemp = lowTempObject.getString("celsius");
  118.  
  119.  
  120. JSONObject dateObject = currentWeather.getJSONObject("date");
  121. String date = dateObject.getString("monthname");
  122. String weekday = dateObject.getString("weekday");
  123. int day = dateObject.getInt("day");
  124. int year = dateObject.getInt("year");
  125.  
  126.  
  127. Weather data = new Weather(maxTemp, minTemp, humidity, date, iconUrl, year, day, weekday);
  128. weather.add(data);
  129.  
  130. }
  131.  
  132.  
  133. } catch(JSONException e){
  134. Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
  135. }
  136.  
  137. return weather;
  138.  
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement