Wednesday, October 25, 2017

Calling Facebook GraphRequest from another class returns null

Leave a Comment

I'm making application that requires to get data from Facebook. To avoid duplicating code I decided to create a class for GraphRequest.

public class FacebookRequest { private static JSONObject object;  private FacebookRequest(JSONObject object) {     this.object = object; }  private static JSONObject GraphApiRequest(String path, AccessToken token){         new GraphRequest(                 token,                 path,                 null,                 HttpMethod.GET,                 new GraphRequest.Callback() {                     public void onCompleted(GraphResponse response) {                         object = response.getJSONObject();                     }                 }         ).executeAsync();     return object; }  public static JSONObject getGraphApi(String path, AccessToken token){     return GraphApiRequest(path, token); }} 

To call the class I use

private static FacebookRequest fbRequest; //.... JSONObject object= fbRequest.getGraphApi(path,token); 

The problem is GraphApiRequest method always returns object=null and only after that executes request.

What should I change to get actual object on call?

EDIT: Thanks to This answer

So I found a solution to get object on call, but it's not perfect option (maybe even wrong, since I am not very experienced in programming, but it works for me)

public class FacebookRequest {     private JSONObject object;      public FacebookRequest(String path, AccessToken token) {         new GraphRequest(                 token,                 path,                 null,                 HttpMethod.GET,                 new GraphRequest.Callback() {                     public void onCompleted(GraphResponse response) {                         object = response.getJSONObject();                     }                 }         ).executeAsync();     }     public JSONObject getObject(){         return object;     } } 

When I am calling request It get's executed after some time

protected void onCreate(Bundle savedInstanceState) {     //...     FacebookRequest fbRequest = new FacebookRequest(path,token);     //... } 

To get actual object on call I use.

JSONObject object = fbRequest.getObject(); 

It is still not working if I call for a JSONObject right after creating constructor. I am looking forward to improve this code, If you will give me some advice.

3 Answers

Answers 1

What you are looking for is explained here (Facebook API how to wait til graphRequest executeAsync is done). As you ask, for use it right after creating constructor replace .executeAsyc() with .executeAndWait().

This is not adviced due a freeze on main thread and a bad user experience.

Answers 2

Instead of calling.private static FacebookRequest fbRequest; Statically, you should call in the constructor, that way your object will not be null and in return of the response stored in object, you will receive data rather than null.

Answers 3

why don't you follow https://developers.facebook.com/docs/android/graph/ this documentation and use this method GraphRequest.newMeRequest()

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment