java - 从 Pincode 到 State 解析数据时出错 org.json.JSONException : Value <! java.lang.String 类型的 DOCTYPE 无法转换为 JSONObject

标签 java android json api

我有这个Java代码:

import org.json.JSONArray;
import org.json.JSONObject;

public class PincodeToState {

private String myState;

public void setPincode(String pin) {

    try {

        JSONObject jsonObj = JsonParser
                .getJSONfromURL("http://api.zippopotam.us/in/" + pin);

        JSONArray Results = jsonObj.getJSONArray("places");

        for (int i = 0; i < Results.length(); i++) {
            JSONObject c = Results.getJSONObject(i);

            myState = c.getString("state");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

public String getState() {
    return myState;

}

}

Json代码

访问http://api.zippopotam.us/in/400031

Logcat错误

07-06 20:41:11.052: E/log_tag(27443): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
07-06 20:41:11.052: E/log_tag(27443): Failed data was:
07-06 20:41:11.052: E/log_tag(27443):     <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML     2.0//EN">
07-06 20:41:11.052: E/log_tag(27443):     <html>
07-06 20:41:11.052: E/log_tag(27443):         <head>
07-06 20:41:11.052: E/log_tag(27443):             <title>Error: 405 Method Not Allowed</title>
07-06 20:41:11.052: E/log_tag(27443):             <style type="text/css">
07-06 20:41:11.052: E/log_tag(27443):               html {background-color: #eee; font-family: sans;}
07-06 20:41:11.052: E/log_tag(27443):               body {background-color: #fff; border: 1px solid #ddd;
07-06 20:41:11.052: E/log_tag(27443):                     padding: 15px; margin: 15px;}
07-06 20:41:11.052: E/log_tag(27443):               pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
07-06 20:41:11.052: E/log_tag(27443):             </style>
07-06 20:41:11.052: E/log_tag(27443):         </head>
07-06 20:41:11.052: E/log_tag(27443):         <body>
07-06 20:41:11.052: E/log_tag(27443):             <h1>Error: 405 Method Not    Allowed</h1>
07-06 20:41:11.052: E/log_tag(27443):             <p>Sorry, the requested URL    <tt>&#039;http://api.zippopotam.us/in/400072&#039;</tt>
07-06 20:41:11.052: E/log_tag(27443):                caused an error:</p>
07-06 20:41:11.052: E/log_tag(27443):             <pre>Method not allowed.</pre>
07-06 20:41:11.052: E/log_tag(27443):         </body>
07-06 20:41:11.052: E/log_tag(27443):     </html>
07-06 20:41:11.052: W/System.err(27443): java.lang.NullPointerException

最佳答案

您正在服务器上发出post请求。请在服务器上执行get请求。然后它就会起作用。

<强> http://www.checkupdown.com/status/E405.html

请访问上面的链接。

我认为这可以解决问题。

关于java - 从 Pincode 到 State 解析数据时出错 org.json.JSONException : Value &lt;! java.lang.String 类型的 DOCTYPE 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24596890/

相关文章:

json - 检索嵌套 Json 数组的第一条记录

python - 读取基于 Web 的 JSON

json - 去解析数组的JSON数组

java - 新的有状态 session bean 实例,无需调用查找

Java 进程消耗超过 2 GB 内存

java - 将 spring-data-elasticsearch 注释与 Jackson ObjectMapper 一起使用?

android - 我的应用程序不会运行,也不会切换 Activity

java - 从独立代码访问队列

java - 如何有效地使用 InterruptedException

java - 在 Android Studio 中制作抽认卡