executeAsync Facebook android SDK
executeAsync Facebook android SDK
我正在调用 request.executeAnsyc 以用信息填充我的数组并显示在列表视图上。遗憾的是,在 request.executeAnsyc 完成之前调用了数组。所以我的 Fragment 的 CreatView 方法没有显示任何内容。
有没有办法在 request.executeAnsyc 完成后使用数组并在列表视图上显示信息?!
此致。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.kizombalistfestival,
container, false);
listUsersInformation = new ArrayList<UserInfo>();
// TODO Auto-generated method stub
Session session = Session.getActiveSession();
new Request(session, "/me", null, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
String serverresponse = response
.getGraphObject().getInnerJSONObject()
.toString();
Log.e("serverresponse", "" + serverresponse);
JSONObject jsonobj;
try {
jsonobj = new JSONObject(serverresponse);
UserInfo temp = new UserInfo(jsonobj
.getString("id"), jsonobj
.getString("first_name"), jsonobj
.getString("gender"), jsonobj
.getString("last_name"), jsonobj
.getString("link"), jsonobj
.getString("locale"), jsonobj
.getString("name"), jsonobj
.getString("timezone"), jsonobj
.getString("updated_time"), jsonobj
.getString("verified"));
listUsersInformation.add(temp);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).executeAsync();
list = (ListView) rootView.findViewById(R.id.list_festivals);
list.setAdapter(new ArrayAdapter<UserInfo>(getActivity(),
android.R.layout.simple_list_item_1, listUsersInformation));
return rootView;
}
我可以想到两种实现方法。
1.使用 Handler 在 finally block
中更新你的列表视图
new Request(session, "/me", null, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
String serverresponse = response
.getGraphObject().getInnerJSONObject()
.toString();
Log.e("serverresponse", "" + serverresponse);
JSONObject jsonobj;
try {
jsonobj = new JSONObject(serverresponse);
UserInfo temp = new UserInfo(jsonobj
.getString("id"), jsonobj
.getString("first_name"), jsonobj
.getString("gender"), jsonobj
.getString("last_name"), jsonobj
.getString("link"), jsonobj
.getString("locale"), jsonobj
.getString("name"), jsonobj
.getString("timezone"), jsonobj
.getString("updated_time"), jsonobj
.getString("verified"));
listUsersInformation.add(temp);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// add your code here
handler.post(new Runnable(){
@Override
public void run(){
list = (ListView)rootView.findViewById(R.id.list_festivals);
list.setAdapter(new ArrayAdapter<UserInfo>(getActivity(),
android.R.layout.simple_list_item_1, listUsersInformation));
}
});
}
}
}).executeAsync();
2。使用广播接收器
首先,您应该在要更新列表视图的片段中注册接收器。然后,在你的 Facebook 函数 class 中,将 Broadcast(intent) 发送到你注册的片段,如下所示:
// your fb request callback
try {
jsonobj = new JSONObject(serverresponse);
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra("id", id);
intent.putExtra("first_name", first_name);
intent.putExtra("gender", gender);
intent.putExtra("last_name", last_name);
// ... and so on
context.sendBroadcast(intent);
}
最终,在您注册广播接收器的片段中,
private final BroadcastReceiver streamReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
///Waking up mobile of it is sleeping
WakeLocker.acquire(getActivity().getApplicationContext());
String id = intent.getExtras().getString("id");
String first_name = intent.getExtras().getString("first_name");
String gender = intent.getExtras().getInt("gender");
...
...
UserInfo temp = new UserInfo(id,
first_name,
gender, .....);
listUsersInformation.add(temp);
yourAdapter.notifyDataSetChanged();
WakeLocker.release();
}
};
我正在调用 request.executeAnsyc 以用信息填充我的数组并显示在列表视图上。遗憾的是,在 request.executeAnsyc 完成之前调用了数组。所以我的 Fragment 的 CreatView 方法没有显示任何内容。
有没有办法在 request.executeAnsyc 完成后使用数组并在列表视图上显示信息?!
此致。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.kizombalistfestival,
container, false);
listUsersInformation = new ArrayList<UserInfo>();
// TODO Auto-generated method stub
Session session = Session.getActiveSession();
new Request(session, "/me", null, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
String serverresponse = response
.getGraphObject().getInnerJSONObject()
.toString();
Log.e("serverresponse", "" + serverresponse);
JSONObject jsonobj;
try {
jsonobj = new JSONObject(serverresponse);
UserInfo temp = new UserInfo(jsonobj
.getString("id"), jsonobj
.getString("first_name"), jsonobj
.getString("gender"), jsonobj
.getString("last_name"), jsonobj
.getString("link"), jsonobj
.getString("locale"), jsonobj
.getString("name"), jsonobj
.getString("timezone"), jsonobj
.getString("updated_time"), jsonobj
.getString("verified"));
listUsersInformation.add(temp);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).executeAsync();
list = (ListView) rootView.findViewById(R.id.list_festivals);
list.setAdapter(new ArrayAdapter<UserInfo>(getActivity(),
android.R.layout.simple_list_item_1, listUsersInformation));
return rootView;
}
我可以想到两种实现方法。
1.使用 Handler 在 finally block
中更新你的列表视图new Request(session, "/me", null, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
String serverresponse = response
.getGraphObject().getInnerJSONObject()
.toString();
Log.e("serverresponse", "" + serverresponse);
JSONObject jsonobj;
try {
jsonobj = new JSONObject(serverresponse);
UserInfo temp = new UserInfo(jsonobj
.getString("id"), jsonobj
.getString("first_name"), jsonobj
.getString("gender"), jsonobj
.getString("last_name"), jsonobj
.getString("link"), jsonobj
.getString("locale"), jsonobj
.getString("name"), jsonobj
.getString("timezone"), jsonobj
.getString("updated_time"), jsonobj
.getString("verified"));
listUsersInformation.add(temp);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// add your code here
handler.post(new Runnable(){
@Override
public void run(){
list = (ListView)rootView.findViewById(R.id.list_festivals);
list.setAdapter(new ArrayAdapter<UserInfo>(getActivity(),
android.R.layout.simple_list_item_1, listUsersInformation));
}
});
}
}
}).executeAsync();
2。使用广播接收器
首先,您应该在要更新列表视图的片段中注册接收器。然后,在你的 Facebook 函数 class 中,将 Broadcast(intent) 发送到你注册的片段,如下所示:
// your fb request callback
try {
jsonobj = new JSONObject(serverresponse);
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra("id", id);
intent.putExtra("first_name", first_name);
intent.putExtra("gender", gender);
intent.putExtra("last_name", last_name);
// ... and so on
context.sendBroadcast(intent);
}
最终,在您注册广播接收器的片段中,
private final BroadcastReceiver streamReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
///Waking up mobile of it is sleeping
WakeLocker.acquire(getActivity().getApplicationContext());
String id = intent.getExtras().getString("id");
String first_name = intent.getExtras().getString("first_name");
String gender = intent.getExtras().getInt("gender");
...
...
UserInfo temp = new UserInfo(id,
first_name,
gender, .....);
listUsersInformation.add(temp);
yourAdapter.notifyDataSetChanged();
WakeLocker.release();
}
};