0

Hello guys so I’m trying to get a list of liked pages by the user. first of all this is how the informations looks like :

{
  "likes": {
      "data": [
                 {
                   "name": "Page Name Here",
                   "id": "Page id",
                   "created_time": "Date"
                 },
                 {
                   "name": "Page Name Here",
                   "id": "Page id",
                   "created_time": "Date"
                 },
                 {
                   "name": "Page Name Here",
                    "id": "Page id",
                   "created_time": "Date"
                  }
                  ....
                  ....
                  ....
}

This is my code :

 loginButton.setReadPermissions("user_likes");

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

        GetPagesUrl = "https://graph.facebook.com/v2.4/me?fields=likes.limit(10000){name}&access_token=" + loginResult.getAccessToken().getToken();
        JSONObject js = new JSONObject(get_json(GetPagesUrl));
        JSONArray jarray = js.getJSONArray("data");
        for(int i = 0; i < jarray.length(); i++){
           // do whatever with jarray.getJSONObject(i).getJSONString("name");
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    });

This is the code of function get_json() used :

public  JSONObject getJson(String url){

    InputStream is = null;
    String result = "";
    JSONObject jsonObject = null;

    // HTTP
    try {           
        HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch(Exception e) {
        return null;
    }

    // Read response to string
    try {           
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();             
    } catch(Exception e) {
        return null;
    }

    // Convert string to object
    try {
        jsonObject = new JSONObject(result);            
    } catch(JSONException e) {
        return null;
    }

    return jsonObject;

}

**unfortunately im testing my app on a real device because i couldnot create AVD so i cant tell you what kinf of error i get **

Thanks :)

jam
  • 9
  • 4
  • 1
    Testing on a real device also gives an error log in your Logcat. Upload that error log. – Faiyaz Sep 27 '15 at 17:01
  • You can check your JSON string with [http://www.jsoneditoronline.org/](http://www.jsoneditoronline.org/) for instance. A comma is missing after each "Page id". – André Blaszczyk Sep 27 '15 at 17:08
  • @AndréBlaszczyk i wrote this as a sample didn't mean to miss this comma. – jam Sep 27 '15 at 17:10
  • Could you share the error message ? – André Blaszczyk Sep 27 '15 at 17:13
  • how could i read the error message ? how to access the logcat @AndréBlaszczyk – jam Sep 27 '15 at 17:19
  • http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Sep 27 '15 at 17:30
  • If you're using a real device, you can try this free application [aLogcat](https://play.google.com/store/apps/details?id=org.jtb.alogcat). You should avoid to loose the error context by logging all exceptions. Add logger.error(e.getMessage(), e) before returning null in all "catch" blocks. – André Blaszczyk Sep 27 '15 at 17:42
  • Are you using eclipse or Android Studio? – Faiyaz Sep 28 '15 at 04:30

3 Answers3

2

I believe this issue is because of

JSONArray jarray = js.getJSONArray("data");

Try accessiing "likes" first , and then the "data" element.

Rami
  • 7,879
  • 12
  • 36
  • 66
VineethaI
  • 36
  • 3
0

You should add a comma after "id": "Page Id" in your JSON scheme.

Ivan
  • 992
  • 1
  • 10
  • 22
0

Usually a good approach for such issues is debugging. Just run your application with debug and use a breakpoint here: JSONObject js = new JSONObject(get_json(GetPagesUrl));

you can post the data of js if it didn't help you to find the answer.

Adi Oz
  • 257
  • 1
  • 4
  • 8