Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. package com.example.hooah.sunshine;
  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. import java.util.List;
  25.  
  26. /**
  27.  * A placeholder fragment containing a simple view.
  28.  */
  29. public class ForecastFragment extends Fragment {
  30.  
  31.     public ForecastFragment() {
  32.     }
  33.  
  34.     @Override
  35.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  36.                              Bundle savedInstanceState) {
  37.         View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  38.  
  39.         String[] forecastArray = {
  40.                 "Today - Sunny - 88/63",
  41.                 "Tomorrow - Foggy - 70/46",
  42.                 "Weds - Cloudy - 72/63",
  43.                 "Thurs - Rainy - 64/51",
  44.                 "Fri - Foggy - 70/46",
  45.                 "Sat - Sunny - 76/68"
  46.         };
  47.  
  48.         List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
  49.  
  50.         ArrayAdapter<String> forecastAdapter = new ArrayAdapter<String>(
  51.                 getActivity(),
  52.                 R.layout.list_item_forecast,
  53.                 R.id.list_item_forecast_textview,
  54.                 weekForecast);
  55.  
  56.         ListView forecastView = (ListView) rootView.findViewById(R.id.listview_forecast);
  57.         forecastView.setAdapter(forecastAdapter);
  58.  
  59.         return rootView;
  60.     }
  61.  
  62.     @Override
  63.     public void onCreate(Bundle savedInstanceState) {
  64.         super.onCreate(savedInstanceState);
  65.         setHasOptionsMenu(true);
  66.     }
  67.  
  68.     @Override
  69.     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ){
  70.         inflater.inflate(R.menu.forecast_fragment, menu);
  71.     }
  72.  
  73.     @Override
  74.     public boolean onOptionsItemSelected(MenuItem item) {
  75.         // Handle action bar item clicks here. The action bar will
  76.         // automatically handle clicks on the Home/Up button, so long
  77.         // as you specify a parent activity in AndroidManifest.xml.
  78.         int id = item.getItemId();
  79.         if (id == R.id.action_refresh) {
  80.             FetchWeatherTask task = new FetchWeatherTask();
  81.             task.doInBackground();
  82.         }
  83.         return super.onOptionsItemSelected(item);
  84.     }
  85.  
  86.     public class FetchWeatherTask extends AsyncTask<Void, Void, Void> {
  87.  
  88.         public final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
  89.  
  90.         public FetchWeatherTask(){
  91.         }
  92.  
  93.         @Override
  94.         protected Void doInBackground(Void... params) {
  95.             // These two need to be declared outside the try/catch
  96.             // so that they can be closed in the finally block.
  97.             HttpURLConnection urlConnection = null;
  98.             BufferedReader reader = null;
  99.             // Will contain the raw JSON response as a string.
  100.             String forecastJsonStr = null;
  101.             try {
  102.                 // Construct the URL for the OpenWeatherMap query
  103.                 // Possible parameters are available at OWM's forecast API page, at
  104.                 // http://openweathermap.org/API#forecast
  105.                 URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=48911&mode=json&units=metric&cnt=7");
  106.                 // Create the request to OpenWeatherMap, and open the connection
  107.                 urlConnection = (HttpURLConnection) url.openConnection();
  108.                 urlConnection.setRequestMethod("GET");
  109.                 urlConnection.connect();
  110.                 // Read the input stream into a String
  111.                 InputStream inputStream = urlConnection.getInputStream();
  112.                 StringBuffer buffer = new StringBuffer();
  113.                 if (inputStream == null) {
  114.                     // Nothing to do.
  115.                     return null;
  116.                 }
  117.                 reader = new BufferedReader(new InputStreamReader(inputStream));
  118.                 String line;
  119.                 while ((line = reader.readLine()) != null) {
  120.                     // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
  121.                     // But it does make debugging a *lot* easier if you print out the completed
  122.                     // buffer for debugging.
  123.                     buffer.append(line + "\n");
  124.                 }
  125.  
  126.                 if (buffer.length() == 0) {
  127.                     // Stream was empty. No point in parsing.
  128.                     return null;
  129.                 }
  130.                 forecastJsonStr = buffer.toString();
  131.             } catch (IOException e) {
  132.                 Log.e(LOG_TAG, "Error ", e);
  133.                 // If the code didn't successfully get the weather data, there's no point in attemping
  134.                 // to parse it.
  135.                 return null;
  136.             } finally {
  137.                 if (urlConnection != null) {
  138.                     urlConnection.disconnect();
  139.                 }
  140.                 if (reader != null) {
  141.                     try {
  142.                         reader.close();
  143.                     } catch (final IOException e) {
  144.                         Log.e(LOG_TAG, "Error closing stream", e);
  145.                     }
  146.                 }
  147.             }
  148.             return null;
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement