0

朋友们。我已经从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 + "}");

这条线由这里给出的指令。在此之后,我的 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("String builder",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>

提前致谢。


你没有得到JSON回应,你得到了一个HTML网页(在 LogCat 中清晰可见)。因此无法解析。

(您的网址以 403 网页响应:)

错误代码:403 禁止。ISA 服务器拒绝了指定的统一资源定位符 (URL)。(12202)

要解决此问题,请使用您可以打开并返回 JSON 数据的正确 URL。

4

1 回答 1

4

你没有得到JSON回应,你得到了一个HTML网页(在 LogCat 中清晰可见)。因此无法解析。

(您的网址以 403 网页响应:)

错误代码:403 禁止。ISA 服务器拒绝了指定的统一资源定位符 (URL)。(12202)

要解决此问题,请使用您可以打开并返回 JSON 数据的正确 URL。

于 2013-03-28T10:53:21.557 回答