Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:Json解析fb图形api url_Android_Json_Facebook Graph Api - Fatal编程技术网

Android:Json解析fb图形api url

Android:Json解析fb图形api url,android,json,facebook-graph-api,Android,Json,Facebook Graph Api,在我的应用程序中,我尝试从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 InputStream

在我的应用程序中,我尝试从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)
获取空指针异常。下面是我的日志

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)
01-2814: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):位于org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-28 14:05:56.384:E/FBUtils(5894):位于org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-28 14:05:56.384:E/FBUtils(5894):位于org.json.JSONObject.(JSONObject.java:154)
01-28 14:05:56.384:E/FBUtils(5894):位于org.json.JSONObject.(JSONObject.java:171)
01-28 14:05:56.384:E/FBUtils(5894):在org.appright.myneighborhood.utils.FBUtils.doInBackground(FBUtils.java:104)
01-28 14:05:56.384:E/FBUtils(5894):在org.appright.myneighborhood.utils.FBUtils.doInBackground(FBUtils.java: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());

您可以看到我们如何使用URLConnection从webservice获取数据的教程,可能是因为缺少花括号?因此,在while中只执行
println
,直到inputLine为
null
。。因此,
NullPointerException
@prospeak iam使用System.out.println(inputLine)将json数据输入输出;现在我无法解析..@teekib:但您正在获取NullPointerException意味着您没有从服务器获取数据,如果获取数据,请确保您获取的字符串能够转换为JSONObject或JSonArray?