我想等到我得到我的用户名和我的 id,也等到我在 facebook 上得到我朋友的用户名和用户 id。我该如何实施?
我在这两个请求之后编写了一个代码,但是有时一个请求没有完成,并且我在其中一个变量中得到 null(例如 userName 变量)
因此我想等到这两个请求完成。
或者也许还有另一个更好的实现?
这是我的代码:
final CountDownLatch isForFinish = new CountDownLatch(1);
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
if( session.isOpened() ){
Request.executeMyFriendsRequestAsync(session, new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users, Response response) {
for (int i=0;i<users.size();i++){
friendsId+= (users.get(i).getId()+",");
friendsName+=(users.get(i).getName()+",");
}
isForFinish.countDown();
}
});
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
String userName = user.getName();
String userId = user.getId();
Intent i = new Intent(getApplicationContext(), TabMainActivity.class);
String email=null;
try {
email = (String) user.getInnerJSONObject().getString("email");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(email!=null){
String newemail=new String(email);
newemail = email.replace("@", "_");
newemail = newemail.replace(".", "_");
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService
(Context.TELEPHONY_SERVICE);
String phoneNumber = mTelephonyMgr.getLine1Number();
String password = "facebook";
ParseUser Puser = new ParseUser();
Puser.setUsername(userId);
Puser.setPassword("facebook");
Puser.setEmail(email);
Puser.put("Name", userName);
try {
isForFinish.await();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Puser.put("friendsId",friendsId );
Puser.put("friendsName",friendsName );
try {
Puser.signUp();
ParseObject saleObj =new ParseObject("sale_"+idOfUser);
saleObj.saveInBackground();
ParseObject deliverObj =new ParseObject("deliver_"+idOfUser);
deliverObj.saveInBackground();
ParseObject group =new ParseObject("group_"+idOfUser);
group.saveInBackground();
ParseObject freind =new ParseObject("freind"+idOfUser);
freind.saveInBackground();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i.putExtra("friendsId", friendsId);
i.putExtra("emailOwner", newemail);
i.putExtra("phone", phoneNumber);
i.putExtra("email",email );
i.putExtra("password",password );
i.putExtra("id",userId );
i.putExtra("name",userName );
startActivity(i);
}
}
});
}