Android Open Source - ThinkPageSDK_Android T P Weather Manager






From Project

Back to project page ThinkPageSDK_Android.

License

The source code is released under:

MIT License

If you think the Android project ThinkPageSDK_Android 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.thinkpage.sdk;
//  w w w. j a  v a  2 s.  c om
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.net.Uri;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class TPWeatherManager 
{
  public class TPJsonHttpResponseHandler extends JsonHttpResponseHandler
  {
    private TPWeatherManagerDelegate _weatherManagerDelegate;
    private TPCity _city;
    public TPJsonHttpResponseHandler(TPWeatherManagerDelegate weatherManagerDelegate, TPCity city)
    {
      _weatherManagerDelegate = weatherManagerDelegate;
      _city = city;
    }
    
      @Override
      public void onSuccess(int statusCode, Header[] headers, JSONObject response)
      {
        TPWeather weather = new TPWeather(response);
          System.out.print(response);
        _weatherManagerDelegate.OnRequestSuccess(_city, weather);
      }

      @Override
      public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) 
      {
        _weatherManagerDelegate.OnRequestFailure(_city, responseString);
        System.out.print(responseString);
      }
  }
  public enum TPWeatherReportLanguage {kEnglish, kSimplifiedChinese, kTraditionalChinese};
  public enum TPTemperatureUnit {kCelsius, kFahrenheit};
  public enum TPAirQualitySource {kAQINone, kAQICity, kAQIAll};
  // ivars
  private String _accessCode;
  private AsyncHttpClient _httpClient;
  private TPWeatherManagerDelegate _delegate;
  private static String kAPISourceCode = "Android2.0.0";
  private static String kBaseThinkPageAPIURL = "https://api.thinkpage.cn/v2/weather/";
  
  public TPWeatherManager(String apiKey, TPWeatherManagerDelegate delegate)
  {
    _accessCode = apiKey;
    _httpClient = new AsyncHttpClient();
    _delegate = delegate;
  }
/*  
  private Uri.Builder URIBuilderWithBasePath()
  {
    //http://api.thinkpage.cn/v1/weather/
    Uri.Builder builder = new Uri.Builder();
    return builder.scheme("http").authority("api.thinkpage.cn").appendPath("v1").appendPath("weather");
  }
  */
  
  public void fetchCurrentWeather(TPCity city, TPWeatherReportLanguage language, TPTemperatureUnit unit)
  {
    RequestParams params = new RequestParams();
    params.put("city", city.description());
    params.put("language", _languageStringFromID(language));
    params.put("unit", _temperatureUnitStringFromID(unit));
    params.put("key", _accessCode);
    params.put("source", kAPISourceCode);
    _fetchDataFromServer(kBaseThinkPageAPIURL + "now.json", params, city);
  }
  
  public void fetchFutureWeather(TPCity city, TPWeatherReportLanguage language, TPTemperatureUnit unit)
  {
    RequestParams params = new RequestParams();
    params.put("city", city.description());
    params.put("language", _languageStringFromID(language));
    params.put("unit", _temperatureUnitStringFromID(unit));  
    params.put("key", _accessCode);
    params.put("source", kAPISourceCode);
    _fetchDataFromServer(kBaseThinkPageAPIURL + "future.json", params, city);
  }

  public void fetchAirQuality(TPCity city, TPWeatherReportLanguage language, TPTemperatureUnit unit, TPAirQualitySource aqi)
  {
    RequestParams params = new RequestParams();
    params.put("city", city.description());
    params.put("language", _languageStringFromID(language));
    params.put("unit", _temperatureUnitStringFromID(unit));
    params.put("aqi", _aqiStringFromID(aqi));
    params.put("key", _accessCode);
    params.put("source", kAPISourceCode);
    _fetchDataFromServer(kBaseThinkPageAPIURL + "air.json", params, city);
  }

  public void fetchWeatherSuggestions(TPCity city, TPWeatherReportLanguage language, TPTemperatureUnit unit)
  {
    RequestParams params = new RequestParams();
    params.put("city", city.description());
    params.put("language", _languageStringFromID(language));
    params.put("unit", _temperatureUnitStringFromID(unit));
    params.put("key", _accessCode);
    params.put("source", kAPISourceCode);
    _fetchDataFromServer(kBaseThinkPageAPIURL + "suggestion.json", params, city);
  }
  
  public void fetchAllWeather(TPCity city, TPWeatherReportLanguage language, TPTemperatureUnit unit, TPAirQualitySource aqi)
  {
    RequestParams params = new RequestParams();
    params.put("city", city.description());
    params.put("language", _languageStringFromID(language));
    params.put("unit", _temperatureUnitStringFromID(unit));  
    params.put("aqi", _aqiStringFromID(aqi));
    params.put("key", _accessCode);
    params.put("source", kAPISourceCode);
    _fetchDataFromServer(kBaseThinkPageAPIURL + "all.json", params, city);
  }
  
  private void _fetchDataFromServer(String urlStr, RequestParams params, TPCity forCity)
  {
    _httpClient.get(urlStr, params, new TPJsonHttpResponseHandler(_delegate, forCity));    
  }
  
  private String _languageStringFromID(TPWeatherReportLanguage languageid)
  {
      switch (languageid)
      {
          case kEnglish:
              return "en";
          case kSimplifiedChinese:
              return "zh-chs";
          case kTraditionalChinese:
              return "zh-cht";
          default:
              return "zh-chs";
      }
  }
  private String _temperatureUnitStringFromID(TPTemperatureUnit unitid)
  {
      switch (unitid)
      {
          case kCelsius:
              return "c";
          case kFahrenheit:
              return "f";
          default:
              return "c";
      }
  }

  private String _aqiStringFromID(TPAirQualitySource aqi)
  {
      switch (aqi)
      {
          case kAQINone:
              return "";
          case kAQICity:
              return "city";
          case kAQIAll:
              return "all";
          default:
              return "";
      }
  }  
}




Java Source Code List

com.example.thinkpageandroid.MainActivity.java
com.loopj.android.http.AsyncHttpClient.java
com.loopj.android.http.AsyncHttpRequest.java
com.loopj.android.http.AsyncHttpResponseHandler.java
com.loopj.android.http.Base64DataException.java
com.loopj.android.http.Base64OutputStream.java
com.loopj.android.http.Base64.java
com.loopj.android.http.BaseJsonHttpResponseHandler.java
com.loopj.android.http.BinaryHttpResponseHandler.java
com.loopj.android.http.DataAsyncHttpResponseHandler.java
com.loopj.android.http.FileAsyncHttpResponseHandler.java
com.loopj.android.http.JsonHttpResponseHandler.java
com.loopj.android.http.JsonStreamerEntity.java
com.loopj.android.http.MySSLSocketFactory.java
com.loopj.android.http.PersistentCookieStore.java
com.loopj.android.http.PreemtiveAuthorizationHttpRequestInterceptor.java
com.loopj.android.http.RangeFileAsyncHttpResponseHandler.java
com.loopj.android.http.RequestHandle.java
com.loopj.android.http.RequestParams.java
com.loopj.android.http.ResponseHandlerInterface.java
com.loopj.android.http.RetryHandler.java
com.loopj.android.http.SerializableCookie.java
com.loopj.android.http.SimpleMultipartEntity.java
com.loopj.android.http.SyncHttpClient.java
com.loopj.android.http.TextHttpResponseHandler.java
com.thinkpage.sdk.TPAirQuality.java
com.thinkpage.sdk.TPCity.java
com.thinkpage.sdk.TPWeatherFuture.java
com.thinkpage.sdk.TPWeatherManagerDelegate.java
com.thinkpage.sdk.TPWeatherManager.java
com.thinkpage.sdk.TPWeatherNow.java
com.thinkpage.sdk.TPWeatherSuggestions.java
com.thinkpage.sdk.TPWeather.java