作者whow (somebody)
看板AndroidDev
標題[問題] 關於facebook的request
時間Fri Aug 23 21:04:23 2013
如題
我寫了一個跟FACEBOOK要好友資料的程式
卻遇到了一個奇怪的問題...
我將從facebook得到的資料存成一個自定class的ArrayList
這個class的資料包含了name, id, picture
而這個arraylist我把他宣告成Activity的member variable
以利之後再其他member function使用
問題就在於...我確定有收到facebook的response
也確定在onComplete裡面有將response存到ArrayList(有print出來測試過)
但我在其他member function要用的時候卻發現size是0...?
也就是我剛剛在onComplete設定的資料沒有存進去 why!!?
我也有將該ArrayList宣告成static了
附上程式碼 麻煩各位了!!
//宣告ArrayList
protected static ArrayList<FriendInfo> friendslist;
public void method(){
.....
getfriendinfo();
System.out.println(friendslist.size()); //結果為0
.....
}
public void getfriendinfo(){
Bundle parm = new Bundle();
parm.putString("fields", "name, picture");
Utility.mAsyncRunner.request("me/friends", parm
, new friendRequestListener());
}
public class friendRequestListener extends BaseRequestListener{
@Overridde
public void onComplete(String response, Object state){
try{
JSONArray friends = new JSONObject(response)
.getJSONArray("data");
if(friends == null)
return;
else{
int i = 0;
FriendInfo tem;
while(friends.opt(i) != null){
//這段比較長 反正就是把response的三個直存到暫存的
FriendInfo class
tem = new FriendInfo(friends.getJSONObject(i).getString("name"),
friends.getJSONObject(i).getString("id"),
friends.getJSONObject(i).getJSONObject("picture").
getJSONObject("data").getString("url"));
//把tem存進ArrayList裡
friendslist.add(i, tem);
i++;
}
}
}catch(JSONException e){
System.out.println(e.getMessage());
}
}
}
public static class FriendInfo{
String name;
String nameID;
String picurl;
FriendInfo(String arg0, String arg1, String arg2){
name = arg0;
nameID = arg1;
picurl = arg2;
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.164.147.116
1F:推 sdyy:跨process? 08/24 11:43
2F:推 linjrming:跟Facebook要求好友這件事是另外一個Thread在跑 08/24 12:20
3F:→ linjrming:你發出要求之後馬上印出size 資料還沒回來當然是0 08/24 12:20
謝謝兩位大大
我後來將向facebook要好友的動作開一個thread去跑
在用wait(), notify()
成功得到值了
這真是我不知道的概念呢 真的很感謝你們!!
※ 編輯: whow 來自: 218.164.132.161 (08/26 09:32)