Android Open Source - Easy-Weather-Android-App Conditions Parser






From Project

Back to project page Easy-Weather-Android-App.

License

The source code is released under:

GNU General Public License

If you think the Android project Easy-Weather-Android-App listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.keepiteasy.easyweather;
/*from  w  w w.  ja  va 2 s  .  c  o m*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;

public class ConditionsParser extends AsyncTask<String, Void, JSONObject> {
  private Activity caller;
  public ConditionsParser(Activity act) {
    caller = act;
  }

  @Override
  protected JSONObject doInBackground(String... params) {
    JSONObject obj = null;
    String link = params[0];

    try {
      URL url = new URL(link);
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      int responseCode = urlConnection.getResponseCode();

      if (responseCode == HttpURLConnection.HTTP_OK) {
        BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line+"\n");
                }
                
                obj = new JSONObject(sb.toString());
      }

      return obj;
    } catch (IOException e) {
      ((LoadingActivity) caller).onError("We are having some trouble reaching our servers.");
      Log.d("Error reading from server", e.getMessage());
      return null;
    } catch (JSONException e) { 
      ((LoadingActivity) caller).onError("We are having some trouble reaching our servers.");
      Log.d("Error parsing JSON", e.getMessage()); return null; 
    }
     
  }

  @Override
  protected void onPostExecute(JSONObject result) {
    if (result != null) {
      Forecast.setConditions(new ConditionsObject(result));
      caller.finish();
    }
  }

}




Java Source Code List

com.keepiteasy.easyweather.AboutActivity.java
com.keepiteasy.easyweather.ConditionsObject.java
com.keepiteasy.easyweather.ConditionsParser.java
com.keepiteasy.easyweather.ErrorActivity.java
com.keepiteasy.easyweather.ForecastObject.java
com.keepiteasy.easyweather.ForecastParser.java
com.keepiteasy.easyweather.Forecast.java
com.keepiteasy.easyweather.LoadingActivity.java
com.keepiteasy.easyweather.LocListener.java
com.keepiteasy.easyweather.WidgetProvider.java