[問題] 關於facebook的request

看板AndroidDev作者 (somebody)時間12年前 (2013/08/23 21:04), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串1/1
如題 我寫了一個跟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

08/24 11:43, , 1F
跨process?
08/24 11:43, 1F

08/24 12:20, , 2F
跟Facebook要求好友這件事是另外一個Thread在跑
08/24 12:20, 2F

08/24 12:20, , 3F
你發出要求之後馬上印出size 資料還沒回來當然是0
08/24 12:20, 3F
謝謝兩位大大 我後來將向facebook要好友的動作開一個thread去跑 在用wait(), notify() 成功得到值了 這真是我不知道的概念呢 真的很感謝你們!! ※ 編輯: whow 來自: 218.164.132.161 (08/26 09:32)
文章代碼(AID): #1I5rtR0U (AndroidDev)
文章代碼(AID): #1I5rtR0U (AndroidDev)