Issue
I am trying to use an AsyncTask to make an API call and map the response to an object I have named MapInfo, which is a variable in the MainActivity. I then use this MapInfo's field, named polyline, and use the prebuilt URL encoder to use in a different API call. The problem I am having is that I have the AsyncTask return the MapInfo object in the PostExecute(), but the code on the MainActivity continues to execute, and I need the MapInfo object to be returned first before continuing down the code.
This is in the MainActivity :
public static MapInfo mapInfo = new MapInfo();
OnCreate() {
button.setonClick() {
//some stuff
new AsyncTask blah = blahblah.execute("");
//the above returns a singular MapInfo object and assigns it to mapInfo
String polyline = mapInfo.polyline; //<--This is where the issue is.
}
}
String polyline is null, and that is used in an API call further down. How should I go about "waiting" for the AsyncTask to finish before assigning polyline to the returned object?
NOTE : The above block is pseudo-code.
EDIT :
I assigned the String, poly, in the post execute, but the same error still occurred. I am doing more than just using the String polyine in the MainActivty, I am using the returned MapInfo object in MainActivity to assign TextViews and then use polyline in a different API call to download an image. Should I change from an AsyncTask to just the API call since I only need 4 fields from the entire JSONobject?
RED arrow is attempt to assign in MainActivity.
BLUE arrow is attempt to assign in OnPostExecute.
snip from Android Studio CLICK ME
Solution
AsyncTask execute the task in background thread which is performed asynchronously.
Two way to get result after task execute:
Use
blahblah.execute("");and get result inside onPostExecute@Override protected void onPostExecute(MapInfo mapInfo) { super.onPostExecute(mapInfo); String polyline = mapInfo.polyline; }OR
Use like
MapInfo mapInfo = blahblah.execute("").get(); String polyline = mapInfo.polyline;In this way no need to catch result in onPostExecute(-)
Answered By - Deven
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.