Exemplo n.º 1
0
  /** 根据传入的代号和类型从服务器上查询省市县数据。 */
  private void queryFromServer(final String code, final String type) {
    String address;
    if (!TextUtils.isEmpty(code)) {
      address = "http://www.weather.com.cn/data/list3/city" + code + ".xml";
    } else {
      address = "http://www.weather.com.cn/data/list3/city.xml";
    }
    showProgressDialog();
    HttpUtil.sendHttpRequest(
        address,
        new HttpCallbackListener() {
          @Override
          public void onFinish(String response) {
            LogUtil.d("ChooseAreaActivity", response);
            boolean result = false;
            if ("province".equals(type)) {
              result = Utility.handleProvincesResponse(coolWeatherDB, response);
            } else if ("city".equals(type)) {
              result =
                  Utility.handleCitiesResponse(coolWeatherDB, response, selectedProvince.getId());
            } else if ("county".equals(type)) {
              result =
                  Utility.handleCountiesResponse(coolWeatherDB, response, selectedCity.getId());
            }
            if (result) {
              // 通过runOnUiThread()方法回到主线程处理逻辑
              runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      closeProgressDialog();
                      if ("province".equals(type)) {
                        queryProvinces();
                      } else if ("city".equals(type)) {
                        queryCities();
                      } else if ("county".equals(type)) {
                        queryCounties();
                      }
                    }
                  });
            }
          }

          @Override
          public void onError(Exception e) {
            // 通过runOnUiThread()方法回到主线程处理逻辑
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    closeProgressDialog();
                    Toast.makeText(ChooseAreaActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
                  }
                });
          }
        });
  }
  private void updateData() {
    Log.d("Service", "循环启动后台更新服务");
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String weatherCode = prefs.getString("weather_code", "");
    String address = "http://www.weather.com.cn/data/cityinfo/" + weatherCode + ".html";

    HttpUtil.sendHttpRequest(
        address,
        new HttpCallbackListener() {

          @Override
          public void onFinish(String response) {

            Utility.handleWeatherResponse(AutoUpdateService.this, response);
          }

          @Override
          public void onError(Exception e) {
            // TODO Auto-generated method stub
            e.printStackTrace();
          }
        });
  }