2013-03-28 114 views
0

之後的預期':'朋友。我已經從AndroidHive下載了一個示例JSON解析項目。org.json.JSONException:在<key

代碼:

// Making HTTP request 
try { 
// defaultHttpClient 
DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpGet httpPost = new HttpGet(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"); 
} 
is.close(); 
json = sb.toString(); //json is String variable. 
Log.i("String Builder", json); 
} 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()); 
} 

運行時,它拋出java.lang.string cannot be converted to JSONObject例外。 因此,對於這個問題的解決搜索後,我改變

 jObj = new JSONObject(json); 

此行

jObj = new JSONObject("{" + json + "}"); 

此行給出here的指導員。在這之後我的logcat顯示以下錯誤。

03-28 16:24:23.722: E/JSON Parser(1516): Error parsing data org.json.JSONException: Expected ':' after <!DOCTYPE at character 12 of {<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
03-28 16:24:23.722: E/JSON Parser(1516): <HTML dir=ltr><HEAD><TITLE>The page cannot be displayed</TITLE> 
03-28 16:24:23.722: E/JSON Parser(1516): <STYLE id=L_defaultr_1>A:link { 
03-28 16:24:23.722: E/JSON Parser(1516): FONT: 8pt/11pt verdana; COLOR: #ff0000 
03-28 16:24:23.722: E/JSON Parser(1516): } 
03-28 16:24:23.722: E/JSON Parser(1516): A:visited { 
03-28 16:24:23.722: E/JSON Parser(1516): FONT: 8pt/11pt verdana; COLOR: #4e4e4e 
03-28 16:24:23.722: E/JSON Parser(1516): } 
03-28 16:24:23.722: E/JSON Parser(1516): </STYLE> 
03-28 16:24:23.722: E/JSON Parser(1516): <META content=NOINDEX name=ROBOTS> 
03-28 16:24:23.722: E/JSON Parser(1516): <META http-equiv=Content-Type content="text-html; charset=UTF-8"> 
03-28 16:24:23.722: E/JSON Parser(1516): <META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD> 
03-28 16:24:23.722: E/JSON Parser(1516): <BODY bgColor=#ffffff> 
03-28 16:24:23.722: E/JSON Parser(1516): <TABLE cellSpacing=5 cellPadding=3 width=410> 
03-28 16:24:23.722: E/JSON Parser(1516): <TBODY> 
03-28 16:24:23.722: E/JSON Parser(1516): <TR> 
03-28 16:24:23.722: E/JSON Parser(1516):  <TD vAlign=center align=left width=360> 
03-28 16:24:23.722: E/JSON Parser(1516):  <H1 id=L_defaultr_2 style="FONT: 13pt/15pt verdana; COLOR: #000000"><ID id=L_defaultr_3><!--Problem-->The page cannot be displayed 
03-28 16:24:23.722: E/JSON Parser(1516): </ID></H1></TD></TR> 
03-28 16:24:23.722: E/JSON Parser(1516): <TR> 
03-28 16:24:23.722: E/JSON Parser(1516):  <TD width=400 colSpan=2><FONT id=L_defaultr_4 
03-28 16:24:23.722: E/JSON Parser(1516):  style="FONT: 8pt/11pt verdana; COLOR: #000000"><ID id=L_defaultr_5><B>Explanation: </B>There is a problem with the page you are trying to reach and it cannot be displayed.</ID></FONT></TD></TR> 
03-28 16:24:23.722: E/JSON Parser(1516): <TR> 
03-28 16:24:23.722: E/JSON Parser(1516):  <TD width=400 colSpan=2><FONT id=L_defaultr_6 
03-28 16:24:23.722: E/JSON Parser(1516):  style="FONT: 8pt/11pt verdana; COLOR: #000000"> 
03-28 16:24:23.722: E/JSON Parser(1516):  <HR color=#c0c0c0 noShade> 
03-28 16:24:23.722: E/JSON Parser(1516):  <P id=L_defaultr_7><B>Try the following:</B></P> 
03-28 16:24:23.722: E/JSON Parser(1516):  <UL> 
03-28 16:24:23.722: E/JSON Parser(1516):   <LI id=L_defaultr_8><B>Refresh page:</B> Search for the page again by clicking the Refresh button. The timeout may have occurred due to Internet congestion. 
03-28 16:24:23.722: E/JSON Parser(1516): <LI id=L_defaultr_9><B>Check spelling:</B> Check that you typed the Web page address correctly. The address may have been mistyped. 
03-28 16:24:23.722: E/JSON Parser(1516): <LI id=L_defaultr_10><B>Access from a link:</B> If there is a link to the page you are looking for, try accessing the page from that link. 
03-28 16:24:23.722: E/JSON Parser(1516):  </UL> 
03-28 16:24:23.722: E/JSON Parser(1516):  <HR color=#c0c0c0 noShade> 
03-28 16:24:23.722: E/JSON Parser(1516):  <P id=L_defaultr_11>Technical Information (for support personnel)</P> 
03-28 16:24:23.722: E/JSON Parser(1516):  <UL> 
03-28 16:24:23.722: E/JSON Parser(1516):   <LI id=L_defaultr_12>Error Code: 403 Forbidden. The ISA Server denied the specified Uniform Resource Locator (URL). (12202) 
03-28 16:24:23.722: E/JSON Parser(1516):   </UL></FONT></TD></TR></TBODY></TABLE></BODY></HTML> 
03-28 16:24:23.722: E/JSON Parser(1516): } 

我已經搜索了很多朋友。但我無法解決這個問題。 所以請幫助我。

注:我在後面ISA服務器工作

我的網址是:http://api.androidhive.info/contacts/

我log.i( 「字符串生成器」,JSON)返回以下。

03-28 16:43:41.903: I/String Builder(1543): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
03-28 16:43:41.903: I/String Builder(1543): <HTML dir=ltr><HEAD><TITLE>The page cannot be displayed</TITLE> 
03-28 16:43:41.903: I/String Builder(1543): <STYLE id=L_defaultr_1>A:link { 
03-28 16:43:41.903: I/String Builder(1543):  FONT: 8pt/11pt verdana; COLOR: #ff0000 
03-28 16:43:41.903: I/String Builder(1543): } 
03-28 16:43:41.903: I/String Builder(1543): A:visited { 
03-28 16:43:41.903: I/String Builder(1543):  FONT: 8pt/11pt verdana; COLOR: #4e4e4e 
03-28 16:43:41.903: I/String Builder(1543): } 
03-28 16:43:41.903: I/String Builder(1543): </STYLE> 
03-28 16:43:41.903: I/String Builder(1543): <META content=NOINDEX name=ROBOTS> 
03-28 16:43:41.903: I/String Builder(1543): <META http-equiv=Content-Type content="text-html; charset=UTF-8"> 
03-28 16:43:41.903: I/String Builder(1543): <META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD> 
03-28 16:43:41.903: I/String Builder(1543): <BODY bgColor=#ffffff> 
03-28 16:43:41.903: I/String Builder(1543): <TABLE cellSpacing=5 cellPadding=3 width=410> 
03-28 16:43:41.903: I/String Builder(1543): <TBODY> 
03-28 16:43:41.903: I/String Builder(1543): <TR> 
03-28 16:43:41.903: I/String Builder(1543):  <TD vAlign=center align=left width=360> 
03-28 16:43:41.903: I/String Builder(1543):  <H1 id=L_defaultr_2 style="FONT: 13pt/15pt verdana; COLOR: #000000"><ID id=L_defaultr_3><!--Problem-->The page cannot be displayed 
03-28 16:43:41.903: I/String Builder(1543): </ID></H1></TD></TR> 
03-28 16:43:41.903: I/String Builder(1543): <TR> 
03-28 16:43:41.903: I/String Builder(1543):  <TD width=400 colSpan=2><FONT id=L_defaultr_4 
03-28 16:43:41.903: I/String Builder(1543):  style="FONT: 8pt/11pt verdana; COLOR: #000000"><ID id=L_defaultr_5><B>Explanation: </B>There is a problem with the page you are trying to reach and it cannot be displayed.</ID></FONT></TD></TR> 
03-28 16:43:41.903: I/String Builder(1543): <TR> 
03-28 16:43:41.903: I/String Builder(1543):  <TD width=400 colSpan=2><FONT id=L_defaultr_6 
03-28 16:43:41.903: I/String Builder(1543):  style="FONT: 8pt/11pt verdana; COLOR: #000000"> 
03-28 16:43:41.903: I/String Builder(1543):  <HR color=#c0c0c0 noShade> 
03-28 16:43:41.903: I/String Builder(1543):  <P id=L_defaultr_7><B>Try the following:</B></P> 
03-28 16:43:41.903: I/String Builder(1543):  <UL> 
03-28 16:43:41.903: I/String Builder(1543):   <LI id=L_defaultr_8><B>Refresh page:</B> Search for the page again by clicking the Refresh button. The timeout may have occurred due to Internet congestion. 
03-28 16:43:41.903: I/String Builder(1543): <LI id=L_defaultr_9><B>Check spelling:</B> Check that you typed the Web page address correctly. The address may have been mistyped. 
03-28 16:43:41.903: I/String Builder(1543): <LI id=L_defaultr_10><B>Access from a link:</B> If there is a link to the page you are looking for, try accessing the page from that link. 
03-28 16:43:41.903: I/String Builder(1543):  </UL> 
03-28 16:43:41.903: I/String Builder(1543):  <HR color=#c0c0c0 noShade> 
03-28 16:43:41.903: I/String Builder(1543):  <P id=L_defaultr_11>Technical Information (for support personnel)</P> 
03-28 16:43:41.903: I/String Builder(1543):  <UL> 
03-28 16:43:41.903: I/String Builder(1543):   <LI id=L_defaultr_12>Error Code: 403 Forbidden. The ISA Server denied the specified Uniform Resource Locator (URL). (12202) 
03-28 16:43:41.903: I/String Builder(1543):   </UL></FONT></TD></TR></TBODY></TABLE></BODY></HTML> 

在此先感謝。

+0

請確保您使用正確的網址獲取網絡服務 – 2013-03-28 10:54:13

+0

我已經提供了我的網址朋友。覈實。 – Gunaseelan 2013-03-28 10:57:06

+0

請使用以下行登錄日誌信息:Log.i(「String Builder」,json); – 2013-03-28 11:01:47

回答

4

您沒有得到JSON響應,您得到一個HTML網頁(在LogCat中清晰可見)。因此它不能被解析。

(網址以403網頁響應:)

錯誤代碼:403禁止。 ISA Server拒絕指定的統一資源定位符(URL) 。 (12202)

要解決此問題,請使用可打開並返回JSON數據的正確URL。

+0

那麼,爲了完美運行我的代碼,我做了哪些改變。 – Gunaseelan 2013-03-28 10:54:32

+0

使用正確的網址 – RvdK 2013-03-28 10:55:13

+0

更改您正在點擊的網址。確保它指向一個正確的Web服務,它將返回一個JSON響應。 – SudoRahul 2013-03-28 10:55:31

相關問題