onPostExecute在task.execute()

时间:2016-03-13 19:51:45

标签: android json android-asynctask

我遇到了AsyncTask的问题

String latlong = "lat=48.8534100&lon=2.3488000";
city = "Paris";
getWeather w = new getWeather(latlong,this);
w.execute();

这个gs.alerter()应该在onpostexecute之后执行,但它是在之前执行的

gs.alerter("thiss" + currentWeather); //This is a Log.i function
gs.cities.add(new City(city, currentWeather + "°", image));
initializeAdapter();

AsyncTask类是:

public class getWeather extends AsyncTask<Void, Void, JSONObject> {

String latlong;
MainActivity obj;
JSONParser jParser = new JSONParser();
JSONObject json;

public getWeather(String latlong,MainActivity obj)
{
    this.latlong = latlong;
    this.obj = obj;
}

@Override
protected JSONObject doInBackground(Void... params) {
    // TODO Auto-generated method stub
    String URL = "http://api.openweathermap.org/data/2.5/weather?" +
            latlong +
            "&appid=APIKEY" +
            "&units=metric";
    json = jParser.getJSONFromUrl(URL);
    return json;
}

@Override
protected void onPostExecute(JSONObject result) {
    this.obj.setWeather(result);
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

}

setweather代码在活动类上:

public void setWeather(JSONObject obj){
    try {
        JSONObject main = obj.getJSONObject("main");
        Double initWeather = new Double(main.getString("temp"));
        int finalWeather = (int)Math.round(initWeather * 1) / 1;
        this.currentWeather = Integer.toString(finalWeather);

此gs.alerter()应该在.execute()

之后立即执行
        gs.alerter(this.currentWeather);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

所以问题是在onpostexecute()之后执行.execute()之后的代码。我想按此顺序执行它: 。执行() onpostexecute()来影响currentWeather变量 然后添加City ...

需要任何帮助。

谢谢大家

PS:我是Android开发的初学者:)

2 个答案:

答案 0 :(得分:1)

  1. 创建界面
  2.   

    public interface OnTaskCompleted {           void onTaskCompleted();       }

    1. 您的活动代码将如下所示

      class YourActivity implements OnTaskCompleted {
      void someFunctionToExecute()
      {
        String latlong = "lat=48.8534100&lon=2.3488000";
        city = "Paris";
        getWeather w = new getWeather(latlong,this);
        w.execute();
      }
      // this code will be called after postExecute
       void OnTaskCompleted(){
          gs.alerter("thiss" + currentWeather); 
          gs.cities.add(new City(city, currentWeather + "°", image));
          initializeAdapter();
       }
      

      }

    2. 您的AsyncTask代码看起来像

    3. public class getWeather extends AsyncTask<Void, Void, JSONObject> {
      String latlong;
      MainActivity obj;
      JSONParser jParser = new JSONParser();
      JSONObject json;
      
      public getWeather(String latlong,MainActivity obj)
      {
          this.latlong = latlong;
          this.obj = obj;
      }
      
      @Override
      protected JSONObject doInBackground(Void... params) {
          // TODO Auto-generated method stub
          String URL = "http://api.openweathermap.org/data/2.5/weather?" +
                  latlong +
                  "&appid=APIKEY" +
                  "&units=metric";
          json = jParser.getJSONFromUrl(URL);
          return json;
      }
      
      @Override
      protected void onPostExecute(JSONObject result) {
          this.obj.setWeather(result);
      }
      
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          obj.OnTaskCompleted(); // calling after postexecute 
      }
      }
      

答案 1 :(得分:1)

  

将此代码放入方法中:

public void methodName() {
    gs.alerter("thiss" + currentWeather); //This is a Log.i function
    gs.cities.add(new City(city, currentWeather + "°", image));
    initializeAdapter();
}
  

现在:

@Override
protected void onPostExecute(JSONObject result) {
    this.obj.setWeather(result);
    obj.methodName();
}