org.json.JSONException:预期':'之后

时间:2013-03-28 10:51:06

标签: android httpclient

友。我从AndroidHive下载了一个示例JSON解析项目。

代码是:

// 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>

提前致谢。

1 个答案:

答案 0 :(得分:4)

您没有收到JSON回复,收到HTML网页(在LogCat中清晰可见)。因此无法解析。

(您的网址以403网页回复:)

  

错误代码:403禁止。 ISA Server拒绝指定的Uniform   资源定位器(URL)。 (12202)

要修复,请使用您可能打开的正确URL并返回JSON数据。