0

I am trying to gets posts data from a Facebook page, but it is showing no data in the JSON object.

Facebook only lets us access data when we have an access token, which I have mentioned in the code. Still, it's returning no data.

I have logged in the facebook and session is active.

fb.authorize(MainActivity.this, new DialogListener() {


            @Override
            public void onComplete(Bundle values) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "OnComplete", Toast.LENGTH_SHORT);
                Editor editor=sp.edit();
                editor.putString("access_token", fb.getAccessToken());
                editor.putLong("access_expires", fb.getAccessExpires());
                editor.commit();
                try {
                    updateButtonImage();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Intent i = new Intent(MainActivity.this, NewsFeed.class);
                startActivity(i);

            }

In NewsFeed.java, I have parsed JSON using this code.

JSONParser jParser = new JSONParser();
    JSONArray jobj = jParser.getJSONFromUrl(url);
    if(jobj==null){
        Log.d("NULLOBJECT", "The object is null");
    }else{
        Log.d("NULLOBJECT", jobj.toString());
    }

JSONParser.java

public class JSONParser {
static InputStream is = null;
  static JSONArray jObj = null;
  static String json = "";
  // constructor
  public JSONParser() {
  }
  public JSONArray getJSONFromUrl(String url) {
    // Making HTTP request
    try {
      // defaultHttpClient
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url);
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
          is, "iso-8859-1"), 8);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line + "n");
      }
      is.close();
      json = sb.toString();
    } catch (Exception e) {
      Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    // try parse the string to a JSON object
    try {
      jObj = new JSONArray(json);
    } catch (JSONException e) {
      Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    // return JSON String
    return jObj;
  }

}

Gdrep
  • 30
  • 7

1 Answers1

0

The most likely explanation is that the stuff you are getting back from Facebook is not JSON. Instead, my guess is that it is an error message encoded as an HTML document.

Modify your code to print out the json string before you attempt to parse it.


I notice that "your" JSONParser class is completely ignoring both the HTTP response status and the response content type.

I've seen that code before; e.g. here and here and a few other places. It is fragile and has examples of bad coding practice, and I strongly recommend that you DON'T use it.

Three lessons here:

  • Don't copy other people's code. Write it yourself.

  • If you do copy, make sure that you get it from a creditable source ...

  • If you do copy, make sure that you thoroughly review what you have copied.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216