Advertisement
MzDee2015

Forecast

Jan 28th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. package com.oluwadara.testagain;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.Menu;
  9. import android.view.MenuInflater;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.ListView;
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.net.HttpURLConnection;
  21. import java.net.URL;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24.  
  25. /**
  26. * Created by oluwadara on 1/28/15.
  27. */
  28. public class ForecastFragment extends Fragment {
  29.  
  30.  
  31.  
  32.  
  33. public ForecastFragment() {
  34. }
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. this.setHasOptionsMenu(true);
  39. }
  40.  
  41. @Override
  42. public boolean onOptionsItemSelected(MenuItem item) {
  43. int id = item.getItemId();
  44. if(id== R.id.action_settings){
  45. FetchWeatherTask task = new FetchWeatherTask();
  46. task.execute();
  47. return true;
  48. }
  49. return super.onOptionsItemSelected(item);
  50. }
  51.  
  52. @Override
  53. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  54. super.onCreateOptionsMenu(menu, inflater);
  55.  
  56. inflater.inflate(R.menu.menu_main, menu);
  57. }
  58.  
  59.  
  60. @Override
  61. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  62. Bundle savedInstanceState) {
  63.  
  64. String[] forecastData = {
  65. "Mon 6/23 - Sunny - 31/17",
  66. "Tue 6/24 - Foggy - 21/8",
  67. "Wed 6/25 - Cloudy - 22/17",
  68. "Thurs 6/26 - Rainy - 18/11",
  69. "Fri 6/27 - Foggy - 21/10",
  70. "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
  71. "Sun 6/29 - Sunny - 20/7"
  72. };
  73.  
  74. ArrayList<String> forecastDataList = new ArrayList<String>(Arrays.asList(forecastData));
  75.  
  76. ArrayAdapter<String> ForecastAdapter = new ArrayAdapter<String>(
  77. //the current context
  78. getActivity(),
  79. //ID of list item layout
  80. R.layout.list_item_forecast,
  81. //ID of the textview to populate
  82. R.id.list_item_forecast_textView,
  83. //forecast data
  84. forecastData
  85. );
  86.  
  87.  
  88.  
  89. View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  90. ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
  91. listView.setAdapter(ForecastAdapter);
  92.  
  93.  
  94. return rootView;
  95. }
  96.  
  97.  
  98. public class FetchWeatherTask extends AsyncTask<Void, Void, Void> {
  99.  
  100. private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
  101.  
  102. @Override
  103. protected Void doInBackground(Void... params) {
  104. // These two need to be declared outside the try/catch
  105. // so that they can be closed in the finally block.
  106. HttpURLConnection urlConnection = null;
  107. BufferedReader reader = null;
  108.  
  109. // Will contain the raw JSON response as a string.
  110. String forecastJsonStr = null;
  111.  
  112. try {
  113. // Construct the URL for the OpenWeatherMap query
  114. // Possible parameters are available at OWM's forecast API page, at
  115. // http://openweathermap.org/API#forecast
  116. URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=Lagos&mode=json&units=metric&cnt=7");
  117.  
  118. // Create the request to OpenWeatherMap, and open the connection
  119. urlConnection = (HttpURLConnection) url.openConnection();
  120. urlConnection.setRequestMethod("GET");
  121. urlConnection.connect();
  122.  
  123. // Read the input stream into a String
  124. InputStream inputStream = urlConnection.getInputStream();
  125. StringBuffer buffer = new StringBuffer();
  126. if (inputStream == null) {
  127. // Nothing to do.
  128. return null;
  129. }
  130. reader = new BufferedReader(new InputStreamReader(inputStream));
  131.  
  132. String line;
  133. while ((line = reader.readLine()) != null) {
  134. // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
  135. // But it does make debugging a *lot* easier if you print out the completed
  136. // buffer for debugging.
  137. buffer.append(line + "\n");
  138. }
  139.  
  140. if (buffer.length() == 0) {
  141. // Stream was empty. No point in parsing.
  142. return null;
  143. }
  144. forecastJsonStr = buffer.toString();
  145. } catch (IOException e) {
  146. Log.e(LOG_TAG, "Error ", e);
  147. // If the code didn't successfully get the weather data, there's no point in attempting
  148. // to parse it.
  149. return null;
  150. } finally {
  151. if (urlConnection != null) {
  152. urlConnection.disconnect();
  153. }
  154. if (reader != null) {
  155. try {
  156. reader.close();
  157. } catch (final IOException e) {
  158. Log.e(LOG_TAG, "Error closing stream", e);
  159. }
  160. }
  161. }
  162. return null;
  163. }
  164.  
  165. }
  166.  
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement