0

I have an issue with AsyncTask

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

This gs.alerter() should be executed after the onpostexecute, but it's execued before

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

The AsyncTask class is :

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();
}

}

The setweather code is on the activity class :

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);

This gs.alerter() should be executed just right after the .execute()

        gs.alerter(this.currentWeather);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

So the problem is that the code just after .execute() is executed after onpostexecute(). I want to execute it in this order : .execute() onpostexecute() to affect the currentWeather variable And then add City ...

Any help is needed.

Thank you all

PS : I am a beginner in Android development :)

ben
  • 398
  • 1
  • 6
  • 15

2 Answers2

1
  1. Create Interface

public interface OnTaskCompleted{ void onTaskCompleted(); }

  1. Your Activity Code will look like

    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. Your AsyncTask Code Will look like

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 
}
}
GaneshP
  • 746
  • 7
  • 25
1

put this code into a method:

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

and now:

@Override
protected void onPostExecute(JSONObject result) {
    this.obj.setWeather(result);
    obj.methodName();
} 
OShiffer
  • 1,366
  • 12
  • 27
  • I just don't know how to thank you ! I've been stuck for hours trying to find a solution ! :) – ben Mar 13 '16 at 20:22