【发布时间】:2012-12-13 15:38:53
【问题描述】:
我在函数中使用 AsyncTask 异步完成执行时如何返回值,因为我不能在这里使用 PostExecute 方法
AsyncTask.execute(new Runnable() {
public void run() {}}
这是代码。 我需要从 SpinnerList 中检索值以在主线程上使用 em。 但是通常每当我要求数据时我都会得到空值,因为它还没有返回值。
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();
}
}});
【问题讨论】:
-
我们不知道你在问什么,你应该详细说明。你想做什么?你试过什么?
-
发布你所有的 AsyncTask 函数以获得更好的描述,你也可以在 AsyncTask 的 onPostExecute 方法中赋值。
-
这是在新线程中执行任务的过时 Android 方法...请参阅下面使用 AsyncTask 的我的帖子,并将
try{}块中的所有内容移动到我的代码中的doInBackground函数内下面...
标签: android