当前位置: 首页 > 知识库问答 >
问题:

组织。json。JSONException:应在

贺自明
2023-03-14

朋友。我从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无法转换为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/

我的日志。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个答案

寿飞飙
2023-03-14

您不会得到JSON响应,而是得到HTML网页(在LogCat中清晰可见)。因此它无法被解析。

(您的url响应为403网页:)

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

要修复此问题,请使用适当的URL,您可以打开该URL并返回JSON数据。

 类似资料:
  • 我正在尝试登录应用程序,但我一直遇到“解析数据org.json.JSONException错误:在字符0的输入结束” 我的登录名。java看起来像这样 公共类登录扩展活动实现OnClickListener{私有EditText用户,pass; 私有静态最终字符串LOGIN_URL="my url"; //从真实服务器进行测试: } } 我的JSONParser。java如下所示:` 公共类JSON

  • 我有一个凌空的JSONRequest,它被卡住了,因为来自服务器的响应不是有效的JSON(它是html)。 如何才能优雅地处理此错误?我有以下实现: 然而,当我发送请求并得到无效的JSON响应时,不会调用onErrorResponse。相反,我得到了一个例外 整个应用程序崩溃了。 我设法将该错误跟踪到JsonObjectRequest.java类中的以下行: 调用的行是:返回响应。错误(new P

  • 目录 必须将所有 state 都维护在 Redux 中吗? 可以用 React 的 setState() 方法吗? 可以将 store 的 state 设置为函数、promise 或者其它非序列化值吗? 如何在 state 中组织嵌套及重复数据? 组织 State 必须将所有 state 都维护在 Redux 中吗? 可以用 React 的 setState() 方法吗? 没有 “标准”。有些用户

  • 作为核心概念, Redux 真的是一种十分简单的设计模式:所有你“写”的逻辑都集中在一个单独的函数中,并且执行这些逻辑的唯一方式就是传给 Redux 一个能够描述当时情景的普通对象(plain object)。Redux store 调用这些逻辑函数,并传入当前的 state tree 以及这些描述对象,返回新的 state tree,接着 Redux store 便开始通知这些订阅者(subsc

  • 以下是组织结构图的示例。 组织结构图有助于呈现节点层次结构,用于描绘组织中的上级/下级关系。 例如,家族树是一种组织结构图。 我们已经在Google Charts Configuration Syntax一章中看到了用于绘制图表的配置 。 现在,让我们看一个组织结构图的例子。 配置 (Configurations) 我们使用OrgChart类来显示组织结构图。 // Organization ch

  • 问题内容: 我当前的应用程序有一个JFrame,其中大约15个动作存储为JFrame中的字段。每个动作都是一个匿名类,其中一些动作相当长。 将动作分解成自己的类是否很常见(可能在称为动作的子包中)? 如果没有,通常如何驯服这种复杂性? 谢谢 问题答案: 如果您的操作可能是可重用的(例如,通过键盘快捷键,其他菜单,其他对话框等),尤其是如果它们可以直接在基础模型上工作(而不是在UI上),那么通常会更