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