0

我想等到我得到我的用户名和我的 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);

                        }
                    }
                });
            }
4

2 回答 2

0

使用 Android Facebook 3.0 设置 Fragment 以使用本教程管理状态

https://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/

您可以使用预建的 facebook 登录按钮也可以使用 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" />

然后要么使用 Session StatusCallback

https://developers.facebook.com/docs/reference/android/3.0/Session.StatusCallback 或您在上一个教程的片段中创建的覆盖,您可以发起调用来检索您的朋友,看起来像这样

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);          
}
于 2013-03-18T12:42:56.180 回答
0

对于依赖线程,您可以使用倒计时锁存器:http: //developer.android.com/reference/java/util/concurrent/CountDownLatch.html

这是一个例子:

http://www.javacodegeeks.com/2011/09/java-concurrency-tutorial.html

于 2013-03-18T09:12:52.517 回答