2013-02-08 63 views
0

我有一个问题,从pinterest得到JSON,它始终是JSONArray部分的错误。我似乎无法从Pinterest JSON获取JSON文件

代码

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; 

} 

} 

*这是与登录JSON = sb.toStirng)所述的logcat(解析器IM;和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 

感谢

+0

请学会为Stack Over Flow格式化您的代码。看看我的编辑。此外,始终提供您的logcat堆栈跟踪。 – Snicolas

+0

嗨,请发布您的logcat跟踪以获得快速响应。 – KunalK

+0

这是链接到图像 - > http://s11.postimage.org/sgsksclf7/Screen_Shot_2013_02_08_at_1_22_33_PM.png – questionair

回答

0

这是一个GET请求。而你使用HttpPost httpPost = new HttpPost(url);.

将其替换为HttpGet httpGet = new HttpGet(url);

+0

谢谢,这工作得很好,只是这1条线让我有一天。 – questionair

+0

@ρяσѕρєяK非常感谢您对这段时间的理解/帮助。 – questionair