等到让我的ID和我freinds ID的Facebook [英] Wait till get my id and my freinds id facebook
问题描述
我想等到我让我的用户名和我的身份证,也等到我得到我freinds的用户名和用户ID在Facebook中。我怎么能实现呢?
我写了一个code这两个请求后,有时却一个请求剪掉光洁度和我在其中一个变量得到空(例如用户名参数)
为此,我想等到这两个请求完成。
或者,也许有另一种更好的实现?
很多
感谢这是我的code:
最后CountDownLatch isForFinish =新CountDownLatch(1); 私有类SessionStatusCallback实现Session.StatusCallback { @覆盖
公共无效呼叫(会话的会话,SessionState会状态,异常除外){
如果(session.isOpened()){ Request.executeMyFriendsRequestAsync(会话,新Request.GraphUserListCallback(){ @覆盖
公共无效onCompleted(列表< GraphUser>的用户,响应响应){
的for(int i = 0; I< users.size();我++){ friendsId + =(users.get(ⅰ).getId()+,);
friendsName + =(users.get(ⅰ).getName()+,); } isForFinish.countDown(); }
});
Request.executeMeRequestAsync(会话,新Request.GraphUserCallback(){ @覆盖
公共无效onCompleted(GraphUser用户,响应响应){ 字符串username = user.getName();
字符串userid = user.getId();
意图I =新意图(getApplicationContext(),TabMainActivity.class);
字符串email = NULL;
尝试{
电子邮件=(字符串)user.getInnerJSONObject()的getString(电子邮件)。
}赶上(JSONException E1){
// TODO自动生成catch块
e1.printStackTrace();
}
如果(电子邮件!= NULL){ 字符串NEWEMAIL =新的String(电子邮件);
NEWEMAIL = email.replace(@,_);
NEWEMAIL = newemail.replace(,_,。); TelephonyManager mTelephonyMgr;
mTelephonyMgr =(TelephonyManager)getSystemService
(Context.TELEPHONY_SERVICE); 串phoneNumber的= mTelephonyMgr.getLine1Number(); 字符串密码=脸谱; ParseUser Puser =新ParseUser();
Puser.setUsername(用户ID);
Puser.setPassword(脸谱);
Puser.setEmail(电子邮件);
Puser.put(名,用户名); 尝试{
isForFinish.await();
}赶上(InterruptedException的E1){
// TODO自动生成catch块
e1.printStackTrace();
}
Puser.put(friendsId,friendsId);
Puser.put(friendsName,friendsName); 尝试{
Puser.signUp();
的parseObject saleObj =新的parseObject(买卖_+ idOfUser);
saleObj.saveInBackground();
的parseObject deliverObj =新的parseObject(交付_+ idOfUser);
deliverObj.saveInBackground();
的parseObject组=新的parseObject(集团_+ idOfUser);
group.saveInBackground();
的parseObject FREIND =新的parseObject(FREIND+ idOfUser);
freind.saveInBackground();
}赶上(ParseException的E){
// TODO自动生成catch块
e.printStackTrace();
}
i.putExtra(friendsId,friendsId);
i.putExtra(emailOwner,NEWEMAIL);
i.putExtra(手机,phoneNumber的);
i.putExtra(邮件,邮件);
i.putExtra(密码,密码);
i.putExtra(ID,用户id);
i.putExtra(名,用户名); startActivity(ⅰ); }
}
});
}
使用Android的Facebook的3.0安装程序的片段使用本教程来管理状态
的https://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/
您可以使用prebuilt Facebook登录按钮也登录使用XML
< com.facebook.widget.LoginButton
机器人:ID =@ + ID / authButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_centerHorizontal =真
机器人:layout_marginBottom =51dp/>
然后要么使用Session StatusCallback
https://developers.facebook.com/docs/reference/安卓/ 3.0 / Session.StatusCallback
或者你在previous教程中的片段创建的覆盖就可以启动一个调用来检索你的朋友这会看起来像这样
无效getFriendsWithApp(最终意向意图){
最后ProgressDialog支持mDialog =新ProgressDialog(本);
mDialog.setMessage(正在加载...);
mDialog.setCancelable(假);
mDialog.show();
字符串fqlQuery =SELECT UID,名称,pic_square FROM用户WHERE UID IN+
(SELECT UID2从相知WHERE UID1 =我());
捆绑PARAMS =新包();
params.putString(Q,fqlQuery);
会话的会话= Session.getActiveSession();
请求请求=新的请求(会话,
/ FQL
参数,可以
HttpMethod.GET,
新Request.Callback(){
公共无效onCompleted(响应响应){
尝试{
mDialog.dismiss();
键入listType =新TypeToken< ArrayList的<&朋友GT;>(){}的getType()。
。Utils.friends =新GSON()fromJson(response.getGraphObject()getInnerJSONObject()getJSONArray(数据)的toString(),listType。);
startActivity(意向);
//这是你会做你想要什么,你检索与您的朋友后,JSON
}赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
}
}
});
Request.executeBatchAsync(请求);
}
i want to wait till i get my user name and my id, and also to wait until i get the user names and users id of my freinds in facebook. how can i implement it?
i wrote a code after these two Request however sometimes one Request didnt finish and i get null in one of the variables (for example the userName variable)
therefor i want to wait till these two requests finish.
Or maybe there is another better implementation?
thanks alot
this is my code:
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);
}
}
});
}
Using Android Facebook 3.0 setup the Fragment to manage the states using this tutorial
https://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/
You can use the prebuilt facebook login button to also login using the xml
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp" />
Then either using the Session StatusCallback
https://developers.facebook.com/docs/reference/android/3.0/Session.StatusCallback or the overrides you created in the fragment in the previous tutorial you can initiate a call to retrieve your friends which would look like this
void getFriendsWithApp(final Intent intent){
final ProgressDialog mDialog = new ProgressDialog(this);
mDialog.setMessage("Loading...");
mDialog.setCancelable(false);
mDialog.show();
String fqlQuery = "SELECT uid, name, pic_square FROM user WHERE uid IN " +
"(SELECT uid2 FROM friend WHERE uid1 = me())";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
try {
mDialog.dismiss();
Type listType = new TypeToken<ArrayList<Friend>>(){}.getType();
Utils.friends = new Gson().fromJson(response.getGraphObject().getInnerJSONObject().getJSONArray("data").toString(), listType);
startActivity(intent);
//This is where you would do what you want after you retrieve your json with friends
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Request.executeBatchAsync(request);
}
这篇关于等到让我的ID和我freinds ID的Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!