Face Book请求后UI不会更新...为什么?

时间:2016-08-29 00:33:56

标签: java android facebook facebook-graph-api textview

我在调用FaceBook请求获取电子邮件后,尝试在TextView userEmail中设置文字。

 public TextView userEmail;

          @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_my_profile);


                //user_profile_name
                userName = (TextView)findViewById(R.id.user_profile_name);
                userEmail = (TextView)findViewById(R.id.user_profile_short_bio);
                userbday = (TextView) findViewById(R.id.user_bday);
        getMyFBProfileRequest();
    }


     public void  getMyFBProfileRequest() {
        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // Application code
                        try {
                            Log.i("Response",response.toString());
                             Toast.makeText(MyProfileActivity.this,object.getString("email") ,
                              Toast.LENGTH_SHORT).show();

                            // Application code
                            String email = object.getString("email");
                            String birthday = object.getString("birthday");


                            userEmail.setText(email);



                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.i("Error","");
                            //profileView.showToast("Error");
                        }
                    }
                });

        // GraphRequest.GraphJSONObjectCallback objectCallback = new JSONObjectCallback();
        //  GraphRequest request = GraphRequest.newMeRequest(accessToken, objectCallback);
        Bundle parameters = new Bundle();
        parameters.putString("fields", "email,name,first_name,last_name,gender");
        request.setParameters(parameters);
        request.executeAsync();

    }

我得到了很好的回应,我在吐司中看到了。 但textView没有任何变化。 UI不更新......为什么?我不知道该怎么办。我使用了广播接收器。失去了很多时间。它不起作用......请帮助我,任何人。

1 个答案:

答案 0 :(得分:0)

FB回答有字段“生日”!但我的请求没有过生日。获取它是正确的:params.putString(“fields”,“birthday”)

params.putString("fields","email,birthday,picture.type(large)");

2)使用JSONObject尝试捕获所有操作:

              try {
                    userModel.setEmail( data.getString("email"));
                } catch (JSONException e) {
                    e.printStackTrace();
                    userModel.setEmail("");
                }

                try {
                    userModel.setBday(data.getString("birthday"));
                } catch (JSONException e) {
                    e.printStackTrace();
                    userModel.setBday("");
                } 

并在cath中设置“”,如果结果为null;

所以,现在我的请求看起来像是:

Bundle params = new Bundle();
        params.putString("fields", "email,birthday,picture.type(large)");

      new GraphRequest(AccessToken.getCurrentAccessToken(),  "/me/", params,      HttpMethod.GET,
                    new GraphRequest.Callback() {
                        public ImageLoader imageLoader;
                        public ImageView mImageView;
                        public UserInfo userModel;

                        @Override
                        public void onCompleted( GraphResponse response) {

                                saveDataInSingletone(response);
                                profileView.setInfoToView();

                        }

                        private void saveDataInSingletone(GraphResponse response)  {
                            JSONObject data = response.getJSONObject();
                            userModel = UserInfo.getInstance();
                            String lastName, firstName;
                            String profilePicUrl;


                            if (data.has("picture")) {
                                try {
                                    profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
                                   // getFacebookProfilePicture(profilePicUrl);

                                   // imageView = (ImageView) findViewById(R.id.pic);
                                   // imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                                    userModel.setAvatar(profilePicUrl);
                                    //mImageView.setImageBitmap(profilePic);
                                   // userModel.setAvatar(profilePic);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                     try {
                            userModel.setEmail( data.getString("email"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                            userModel.setEmail("");
                        }

                        try {
                            userModel.setBday(data.getString("birthday"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                            userModel.setBday("");
                        }}).executeAsync();