Android Open Source - Weather Web Access Tools






From Project

Back to project page Weather.

License

The source code is released under:

GNU General Public License

If you think the Android project Weather 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.imlongluo.weather.utils;
//from   w w  w  .  j a va2 s .c o m
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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 android.content.Context;
import android.widget.Toast;

/**  
 * 
 * ??????????Android?????
 ??????????Android?????
 * 
 */
public class WebAccessTools {

  /**
   * ????Context?????
   */
  private Context context;

  /**
   * ???????????
   * @param context ?????Activity??Context?????
   */
  public WebAccessTools(Context context) {
    this.context = context;
  }

  /**
   * ???????url????????????????(???GET?????)
   * @param url ???url????
   * @return web????????????<code>String</code>?????????????null
   */
  public String getWebContent(String url) {
    // ????http????
    HttpGet request = new HttpGet(url);
    // ??HttpParams???????HTTP????
    HttpParams params = new BasicHttpParams();
    // ????????????
    // HttpConnectionParams.setConnectionTimeout(params, 3000);
    // HttpConnectionParams.setSoTimeout(params, 5000);
    // ??????????????
    HttpClient httpClient = new DefaultHttpClient(params);
    try {
      // ?????????
      HttpResponse response = httpClient.execute(request);
      // ???????????
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        // ?????????
        String content = EntityUtils.toString(response.getEntity());
        return content;
      } else {
        // ????????Toast??????????
        Toast.makeText(context, "????????????????????!",
            Toast.LENGTH_LONG).show();
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // ?????????
      httpClient.getConnectionManager().shutdown();
    }
    return null;
  }
}




Java Source Code List

.WebAccessTools.java
com.imlongluo.weather.app.MainActivity.java
com.imlongluo.weather.app.SetCityActivity.java
com.imlongluo.weather.app.UpdateWidgetService.java
com.imlongluo.weather.app.WeatherWidget.java
com.imlongluo.weather.db.DBHelper.java
com.imlongluo.weather.location.GPSListAdapter.java
com.imlongluo.weather.location.MyListAdapter.java
com.imlongluo.weather.utils.LocationXMLParser.java
com.imlongluo.weather.utils.WeaterInfoParser.java