0

在我的应用程序中,我试图从 fb graph api url 检索数据,如下所示

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 );

我得到 json 格式的输出,但我无法读取消息,在这一行出现错误,出现JSONObject json = new JSONObject(inputLine); nullpointer 异常。下面是我的日志

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)

任何帮助表示赞赏。

4

1 回答 1

1

使用 StringBuilder 而不是 String 从 InputStream 读取数据并尝试从 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());
于 2013-01-28T09:03:06.343 回答