nnnn nnnn - 3 months ago 13
Android Question

UI does not update after Face Book request...why?

I am trying to set text in

TextView
userEmail
, after calling FaceBook request for getting Email.

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();

}


And I get a fine response, which I see in toast.
But nothing change in
textView
. UI does not update… why? I do not know what to do. I used Broadcast Receiver. lost a lot of time. It does not work… Help me, please, anybody.

Answer

FB answer has field "birthday"! But my request did't get birthday. It is correct for get it: params.putString("fields", "birthday")

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

2) To surround with try catch all operation with 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("");
                } 

and set "" in cath if result null;

So, now my request looks like:

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();