【问题标题】:Error: <!DOCTYPE of type java.lang.String cannot be converted to JSONObject错误:<!DOCTYPE of type java.lang.String 无法转换为 JSONObject
【发布时间】:2016-12-25 06:18:42
【问题描述】:

我正在开发 android 项目在调用 api 时,它会抛出如下异常。我认为一切都做对了,但我厌倦了这个例外。

org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

这是我的代码

public class APIConnectionManager {

    private static int CONNECTION_TIMEOUT = 60000;
    static JSONObject response = null;
    public static final int GET = 1;
    public static final int POST = 2;


    /**
     * Function to place a get call to the customer profile API that returns a JSONObject
     *
     * @param url     - The path to the resource
     * @param params - A list of name value pairs
     * @return    - JSONObject
     */
    public JSONObject doCustomerAPIGet(String url, int method, List<NameValuePair> params) {

        try{

            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // check the http method request type
            if(method==POST){

                HttpPost httpPost = new HttpPost(url);

                // adding post params
                if(params!=null){

                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);
            }else if (method==GET){

                // appending params to the url
                if (params!=null) {
                    String paramString = URLEncodedUtils.format(params, "utf-8");

                    url += "?" + paramString;
                }

                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);
            }

            httpEntity = httpResponse.getEntity();

            String result = EntityUtils.toString(httpEntity);

            response = new JSONObject(result);


        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e){
            e.printStackTrace();
        }

        return response;

    }
}

错误指向这一行

response = new JSONObject(result);

同样得到回应

{"data":{"customerno":1339451,"loyalty_id":"9700354522","firstname":"raju","lastname":"king","email":"rajucse45@gmail.com","mobile":"9700354522","address":"1st street","city":"chennai","pincode":"600028","links":[]},"status":"success"}

有人可以就此提出建议吗?

【问题讨论】:

  • 咨询您的后端开发人员
  • 请看我上面的回复
  • &lt;!DOCTYPE 可能表明您收到的是HTML 而不是JSON。在调试模式下运行以验证您得到的响应。
  • 我看到后评论说,您的 api 中存在一些导致此问题的错误,如果您缺少任何参数,也可能发生这种情况(尽管 api 应该使用适当的错误消息来处理)

标签: android api exception jsonobject


【解决方案1】:

您没有收到可以转换为 JSON 的响应。 您的 API 响应中有错误,因此只需调试您的应用并将错误显示给您的 API 开发人员。

【讨论】:

    猜你喜欢
    • 2016-11-24
    • 2016-05-27
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多