Back to project page CoolWeather.
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.
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; } }