0

In my app i am trying to retrieve the data from a fb graph api url as below

URL fbmsg = new URL("https://graph.facebook.com/"+FbPostId()+"?access_token="+TOKEN+"");
            URLConnection yc = fbmsg.openConnection();

            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

            String inputLine;

            String s = "";

            while ((inputLine = in.readLine()) != null)

            System.out.println(inputLine);
            //Log.d(TAG, "getPostId trace getFbPostId " + inputLine);   
            s = s + inputLine + "n";
            //Log.d(TAG, "getPostId trace getFbPostId " + s);   
            in.close();
            //System.out.println(s);

            JSONObject json = new JSONObject(inputLine);
            //JSONObject message = json.getJSONObject("message");
           String fbmessage = json.getString("message");
            System.out.println( "message: " + fbmessage );

i am getting the output in json format but i am unable to read the message , getting error at this line JSONObject json = new JSONObject(inputLine); getting nullpointer exception . below is my log

01-28 14:05:56.384: E/FBUtils(5894): getPostId
01-28 14:05:56.384: E/FBUtils(5894): java.lang.NullPointerException
01-28 14:05:56.384: E/FBUtils(5894):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-28 14:05:56.384: E/FBUtils(5894):    at org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-28 14:05:56.384: E/FBUtils(5894):    at org.json.JSONObject.<init>(JSONObject.java:154)
01-28 14:05:56.384: E/FBUtils(5894):    at org.json.JSONObject.<init>(JSONObject.java:171)
01-28 14:05:56.384: E/FBUtils(5894):    at org.appright.myneighborhood.utils.FBUtils.doInBackground(FBUtils.java:104)
01-28 14:05:56.384: E/FBUtils(5894):    at org.appright.myneighborhood.utils.FBUtils.doInBackground(FBUtils.java:1)

Any help is appreciated.

4
  • you can see vogella.com/articles/AndroidNetworking/article.html tutorial how we get data from webservice using URLConnection Commented Jan 28, 2013 at 8:56
  • Maybe it is because of your missing curly brackets? so only the println is executed inside the while, until inputLine is null.. therefore the NullPointerException Commented Jan 28, 2013 at 9:01
  • @prospeak iam getting the json data in out put with System.out.println(inputLine); now iam unable to parse.. Commented Jan 28, 2013 at 9:03
  • @teekib : but u are getting NullPointerException means you are not getting data from server and if getting it then make sure you are getting string which is convert-able to JSONObject or JSonArray? Commented Jan 28, 2013 at 9:05

1 Answer 1

1

use StringBuilder instead of String for reading data from InputStream and try as to read data from InputStream:

StringBuilder inputLine = new StringBuilder();
String s;
String NL = System.getProperty("line.separator");
while ((s = in.readLine()) != null) {
   inputLine.append(s + NL);
}
in.close();
JSONObject json = new JSONObject(inputLine.toString());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.