Android Open Source - CoolWeather Weather Http Client






From Project

Back to project page CoolWeather.

License

The source code is released under:

GNU General Public License

If you think the Android project CoolWeather 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 org.das.coolweather.utils;
/*  w w w.j  av  a  2s  . c  o  m*/
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;

public class WeatherHttpClient {
  public static String UNITS;
  public static String PRED_LANG;
  private static final String APPID = "670d7fd5ed6be7af97f71f698ba1aad2";
  private static final String MODE = "json";
  private static final String BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?APPID="+APPID+"&";
  

  public static JSONObject getDataFromLocation(String optionString) {
    
    try {
      return new AsyncTask<String, Void, JSONObject>() {

        @Override
        protected JSONObject doInBackground(String... params) {
          try {
            HttpParams httpParameters = new BasicHttpParams();
            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
            HttpConnectionParams.setSoTimeout(httpParameters, 15000);
            
            HttpGet httpget = new HttpGet(BASE_URL + params[0]);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity);
            JSONObject data = new JSONObject(result);
            return data;
          } catch (UnsupportedEncodingException e) {
            return null;
          } catch (ClientProtocolException e) {
            return null;
          } catch (IOException e) {
            return null;
          } catch (JSONException e) {
            return null;
          }
        }
        
      }.execute(optionString+"&mode=" + MODE + "&units="+UNITS +"&lang="+PRED_LANG).get();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      e.printStackTrace();
    }
    return null;
    
  }
}




Java Source Code List

org.das.coolweather.activities.DetailsActivityHost.java
org.das.coolweather.activities.SettingsActivity.java
org.das.coolweather.adapters.DayInfo.java
org.das.coolweather.adapters.DayListAdapter.java
org.das.coolweather.fragmentactivities.MainActivity.java
org.das.coolweather.fragments.Details.java
org.das.coolweather.fragments.Graph.java
org.das.coolweather.fragments.Map.java
org.das.coolweather.fragments.Search.java
org.das.coolweather.utils.LaBD.java
org.das.coolweather.utils.WeatherHttpClient.java