简体   繁体   English

如何修复“价值

[英]How to fix "Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject" error in Android?

I try to send token to server side in my application in header.我尝试在我的应用程序中将令牌发送到服务器端。 but when I try to send token I get this error但是当我尝试发送令牌时,我收到此错误

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

my server side is laravel.我的服务器端是 Laravel。

Even I tried using utf-8 but it didn't work即使我尝试使用utf-8但它没有用

con.addRequestProperty("Authorization", "Bearer " + token+"; charset=utf-8" );

this is my code when I connect to server这是我连接到服务器时的代码

@Override
protected Object doInBackground(Object[] objects) {

    try {
        URL url = new URL(objects[URL_ADDRESS].toString());
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod(objects[CONNECTION_METHOD].toString());

        if (token != null) {
            con.addRequestProperty("Authorization", "Bearer " + token+"; charset=utf-8" );
        }

        if (objects[CONNECTION_METHOD].toString().equals("POST")) {
            con.addRequestProperty("Content-Type", "application/json; charset=utf-8");
            con.setDoInput(true);
            con.setDoOutput(true);
            OutputStream out = con.getOutputStream();
            out.write(objects[OUTPUT_OBJECT].toString().getBytes("UTF-8"));
        }

        String str;
        if (con.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
            str = getRespons(con.getInputStream()) /*+ jsRespons*/;
        } else {
            str = getRespons(con.getErrorStream()) /*+ jsRespons*/;
        }

        ///

        Map<String, List<String>> headerFields = con.getHeaderFields();

        Set<String> headerFieldsSet = headerFields.keySet();
        Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();

        while (hearerFieldsIter.hasNext()) {

        String headerFieldKey = hearerFieldsIter.next();
        List<String> headerFieldValue = headerFields.get(headerFieldKey);

        StringBuilder sb = new StringBuilder();
        for (String value : headerFieldValue) {
            sb.append(value);
            sb.append("");
        }
            System.out.println(headerFieldKey + "=" + sb.toString() );
        }

        ///

        JSONObject jsResponse = new JSONObject(str);
        jsResponse.put("resMsg", con.getResponseMessage());
        jsResponse.put("response", con.getResponseCode());

        return jsResponse.toString();

    } catch (Exception e) {
        return e.toString();
    }

}

thanks.谢谢。

根据Laravel 文档Accept: application/json标头添加到您的请求中。

con.addRequestProperty("Accept", "application/json");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android错误org.json.JSONException:值 - android error org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 值 - Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 解析dataorg.json.JSONException:值时出错 - Error parsing dataorg.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 如何解决org.json.JSONException:Value - How to solve the org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 价值 - value <!DOCTYPE of type java.lang.string can not be converted to jsonobject 值[{类型为java.lang.String的值无法在android中转换为JSONObject - Value [{ of type java.lang.String cannot be converted to JSONObject in android 值 - Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject, FATAL EXCEPTION: AsyncTask #1 Android-JSP org.json.JSONException:值 - Android-JSP org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 解析数据org.json.JSONException的另一个错误:值 - Another Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 随机获取org.json.JSONException错误:值 - Getting randomly error of org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM