i use AsyncTask inside a function
How to return a value when Async Finish Execution , since i cant use PostExecute method here
AsyncTask.execute(new Runnable() {
public void run() {}}
Heres The Code.
I need to retrieve values from SpinnerList to use em on the main thread.
But usually i get null values whenever i ask for data , because it didnt return the values yet.
AsyncTask.execute(new Runnable() {
public void run() {
try{
String fql = "SELECT uid, name FROM user WHERE is_app_user AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
Bundle parameters = new Bundle();
parameters.putString("query", fql);
parameters.putString("method", "fql.query");
parameters.putString("access_token", fb.getAccessToken());
String Response = fb.request(parameters);
JSONArray json = new JSONArray(Response);
list = new ArrayList<String[][]>();
String[][] friendsToList = new String[1][2];
SpinnerList = new ArrayList<String>();
String TempToSpinnerList=new String();
for(int i = 0; i < json.length(); ++i){
friendsToList[0][0] = json.getJSONObject(i).getString("uid");
friendsToList[0][1] = json.getJSONObject(i).getString("name");
TempToSpinnerList= friendsToList[0][1];
list.add(friendsToList);
SpinnerList.add(TempToSpinnerList);
Log.e("Test"," "+friendsToList[0][1]+" "+friendsToList[0][0]);
}
//dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner.setAdapter(dataAdapter);
/* jsonUser = fb.request(parameters);
friendObj = Util.parseJson(jsonUser);
friendArray = friendObj.getJSONArray("data");
//
friendlist = new ArrayList<String[][]>();
String[][] friendsToList = new String[1][2];
int friendCount = 0;
String fId, fNm;
JSONObject friend;
for (int i = 0;i<friendArray.length();i++){
//Get a JSONObject from the JSONArray
friend = friendArray.getJSONObject(i);
//Extract the strings from the JSONObject
friendsToList[0][0] = friend.getString("id");
friendsToList[0][1] = friend.getString("name");
friendlist.add(friendsToList);
//Set the values to our arrays
Log.e("Tester",""+ friendsToList[0][0]+" "+ friendsToList[0][1]);
friendCount ++;}*/
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
If you want to perform some function once the Asynctask is complete, you can setup a callback function in the calling activity. Create a constructor in the AsyncTask class and pass it the calling activity. Then use this reference to call the callback function when your task is complete.
You can also update the UI from within onPostExecute, which is simpler if it works for you.
MainActivity.java