我在从 pinterest 获取 json 时遇到问题,它总是在 JSONArray 部分出错。
代码
JSONArray bodyarray = null;
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
Log.i("url",""+url);
Log.i("json",""+json);
try{
bodyarray = json.getJSONArray(TAG_body); <---- this is the error here "NullPointerException"
Log.i("JSON", ""+bodyarray);
}
收到 JSON
网址 - http://pinterestapi.co.uk/username/pins
从 facebook 和 twitter 解析 JSON 时我没有遇到这个问题,这只发生在 pinterest 链接中。如何在 pinterest 链接上获取数组?
这是我正在使用的解析器
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject 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"); //\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 JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
*这是带有 Log on json = sb.toStirng(); 的 logcat;和 str_final*
02-08 15:09:13.032: I/json sbtostring(416): <!DOCTYPE html>
02-08 15:09:13.032: I/json sbtostring(416): <html>
02-08 15:09:13.032: I/json sbtostring(416): <head>
02-08 15:09:13.032: I/json sbtostring(416): <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
02-08 15:09:13.032: I/json sbtostring(416): <meta name="robots" content="noindex,nofollow" />
02-08 15:09:13.032: I/json sbtostring(416): <title>Whoops, looks like something went wrong.</title>
02-08 15:09:13.032: I/json sbtostring(416): <style>
02-08 15:09:13.032: I/json sbtostring(416): /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
02-08 15:09:13.032: I/json sbtostring(416): html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}
02-08 15:09:13.032: I/json sbtostring(416): html { background: #eee; padding: 10px }
02-08 15:09:13.032: I/json sbtostring(416): body { font: 11px Verdana, Arial, sans-serif; color: #333 }
02-08 15:09:13.032: I/json sbtostring(416): img { border: 0; }
02-08 15:09:13.032: I/json sbtostring(416): .clear { clear:both; height:0; font-size:0; line-height:0; }
02-08 15:09:13.032: I/json sbtostring(416): .clear_fix:after { display:block; height:0; clear:both; visibility:hidden; }
02-08 15:09:13.032: I/json sbtostring(416): .clear_fix { display:inline-block; }
02-08 15:09:13.032: I/json sbtostring(416): * html .clear_fix { height:1%; }
02-08 15:09:13.032: I/json sbtostring(416): .clear_fix { display:block; }
02-08 15:09:13.032: I/json sbtostring(416): #content { width:970px; margin:0 auto; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset, .sf-exceptionreset .block { margin: auto }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset abbr { border-bottom: 1px dotted #000; cursor: help; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset p { font-size:14px; line-height:20px; color:#868686; padding-bottom:20px }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset strong { font-weight:bold; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset a { color:#6c6159; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset a img { border:none; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset a:hover { text-decoration:underline; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset em { font-style:italic; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset h1, .sf-exceptionreset h2 { font: 20px Georgia, "Times New Roman", Times, serif }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset h2 span { background-color: #fff; color: #333; padding: 6px; float: left; margin-right: 10px; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset .traces li { font-size:12px; padding: 2px 4px; list-style-type:decimal; margin-left:20px; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset .block { background-color:#FFFFFF; padding:10px 28px; margin-bottom:20px;
02-08 15:09:13.032: I/json sbtostring(416): -webkit-border-bottom-right-radius: 16px;
02-08 15:09:13.032: I/json sbtostring(416): -webkit-border-bottom-left-radius: 16px;
02-08 15:09:13.032: I/json sbtostring(416): -moz-border-radius-bottomright: 16px;
02-08 15:09:13.032: I/json sbtostring(416): -moz-border-radius-bottomleft: 16px;
02-08 15:09:13.032: I/json sbtostring(416): border-bottom-right-radius: 16px;
02-08 15:09:13.032: I/json sbtostring(416): border-bottom-left-radius: 16px;
02-08 15:09:13.032: I/json sbtostring(416): border-bottom:1px solid #ccc;
02-08 15:09:13.032: I/json sbtostring(416): border-right:1px solid #ccc;
02-08 15:09:13.032: I/json sbtostring(416): border-left:1px solid #ccc;
02-08 15:09:13.032: I/json sbtostring(416): }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset .block_exception { background-color:#ddd; color: #333; padding:20px;
02-08 15:09:13.032: I/json sbtostring(416): -webkit-border-top-left-radius: 16px;
02-08 15:09:13.032: I/json sbtostring(416): -webkit-border-top-right-radius: 16px;
02-08 15:09:13.032: I/json sbtostring(416): -moz-border-radius-topleft: 16px;
02-08 15:09:13.032: I/json sbtostring(416): -moz-border-radius-topright: 16px;
02-08 15:09:13.032: I/json sbtostring(416): border-top-left-radius: 16px;
02-08 15:09:13.032: I/json sbtostring(416): border-top-right-radius: 16px;
02-08 15:09:13.032: I/json sbtostring(416): border-top:1px solid #ccc;
02-08 15:09:13.032: I/json sbtostring(416): border-right:1px solid #ccc;
02-08 15:09:13.032: I/json sbtostring(416): border-left:1px solid #ccc;
02-08 15:09:13.032: I/json sbtostring(416): }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset li a { background:none; color:#868686; text-decoration:none; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset li a:hover { background:none; color:#313131; text-decoration:underline; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset ol { padding: 10px 0; }
02-08 15:09:13.032: I/json sbtostring(416): .sf-exceptionreset h1 { background-color:#FFFFFF; padding: 15px 28px; margin-bottom: 20px;
02-08 15:09:13.032: I/json sbtostring(416): -webkit-border-radius: 1
02-08 15:09:13.042: E/JSON Parser(416): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-08 15:09:13.063: E/JSON Parser(416): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-08 15:09:13.063: I/url(416): http://pinterestapi.co.uk/username/pins
02-08 15:09:13.072: I/json(416): null
02-08 15:09:13.072: D/AndroidRuntime(416): Shutting down VM
02-08 15:09:13.082: W/dalvikvm(416): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-08 15:09:13.102: E/AndroidRuntime(416): FATAL EXCEPTION: main
02-08 15:09:13.102: E/AndroidRuntime(416): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AR.ARGroup/com.AR.ARGroup.Social_Pinterest}: java.lang.NullPointerException
02-08 15:09:13.102: E/AndroidRuntime(416): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-08 15:09:13.102: E/AndroidRuntime(416): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-08 15:09:13.102: E/AndroidRuntime(416): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 15:09:13.102: E/AndroidRuntime(416): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-08 15:09:13.102: E/AndroidRuntime(416): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 15:09:13.102: E/AndroidRuntime(416): at android.os.Looper.loop(Looper.java:123)
02-08 15:09:13.102: E/AndroidRuntime(416): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 15:09:13.102: E/AndroidRuntime(416): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 15:09:13.102: E/AndroidRuntime(416): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 15:09:13.102: E/AndroidRuntime(416): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 15:09:13.102: E/AndroidRuntime(416): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 15:09:13.102: E/AndroidRuntime(416): at dalvik.system.NativeStart.main(Native Method)
02-08 15:09:13.102: E/AndroidRuntime(416): Caused by: java.lang.NullPointerException
02-08 15:09:13.102: E/AndroidRuntime(416): at com.AR.ARGroup.Social_Pinterest.onCreate(Social_Pinterest.java:76)
02-08 15:09:13.102: E/AndroidRuntime(416): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 15:09:13.102: E/AndroidRuntime(416): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-08 15:09:13.102: E/AndroidRuntime(416): ... 11 more
谢谢