Search code examples
androidfacebook-graph-apifacebook-android-sdk

Facebook Graph API Searching on Android


I use the following to code to retrieve data from Facebook Graph API and it works.

GraphRequest.newGraphPathRequest(AccessToken.getCurrentAccessToken()
                        , "/me"
                        , new GraphRequest.Callback() {
                            @Override
                            public void onCompleted(GraphResponse response) {
                                showToast(response.toString());
                                ((TextView) findViewById(R.id.result_textview)).setText(response.toString());
                            }
                        }).executeAsync();

However, when I use the following search query which works in Graph API Explorer it doesn't work anymore.

GraphRequest.newGraphPathRequest(AccessToken.getCurrentAccessToken()
                        , "/search?q=coffee&type=place"
                        , new GraphRequest.Callback() {
                            @Override
                            public void onCompleted(GraphResponse response) {
                                showToast(response.toString());
                                ((TextView) findViewById(R.id.result_textview)).setText(response.toString());
                            }
                        }).executeAsync();

The error json is below.

Response:responseCode:400,
graphObject:null,
error:{  
   HttpStatus:400,
   errorCode:100,
   errorType:GraphMethodException,
   errorMessage:   Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api
}
}

Solution

  • Please read the documentation. You are doing it the wrong way.

    new GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "...?fields={fieldname_of_type_Location}",
        null,
        HttpMethod.GET,
        new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                /* handle the result */
            }
        }
    ).executeAsync()
    

    OR

    GraphRequest request = GraphRequest.newGraphPathRequest(
      accessToken,
      "/search",
      new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
          // Insert your code here
        }
    });
    
    Bundle parameters = new Bundle();
    parameters.putString("type", "place");
    parameters.putString("center", "53,27");
    parameters.putString("distance", "30000");
    request.setParameters(parameters);
    request.executeAsync();
    

    In field you can use country, longitude, latitude , cite etc. Here is docs

    Hope this would be helpful for you.