作者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)