Php org.json.JSONException:Value<;!接收有效的JSON响应时,无法将java.lang.String类型的DOCTYPE转换为JSONObject

Php org.json.JSONException:Value<;!接收有效的JSON响应时,无法将java.lang.String类型的DOCTYPE转换为JSONObject,php,android,json,Php,Android,Json,我正在尝试读取有效的JSON响应,但获取的错误字符串无法转换为JSONObject。我不知道为什么 android代码 String sendParam = sendParams[0]; byte[] sendParamsByte = sendParam.getBytes("UTF-8"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); con

我正在尝试读取有效的JSON响应,但获取的错误字符串无法转换为JSONObject。我不知道为什么

android代码

String sendParam = sendParams[0];

byte[] sendParamsByte = sendParam.getBytes("UTF-8");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(sendParamsByte.length));
conn.setDoOutput(true);
conn.getOutputStream().write(sendParamsByte);

InputStream responseInputStream = conn.getInputStream();
StringBuffer responseStringBuffer = new StringBuffer();
byte[] byteContainer = new byte[1024];

for (int i; (i = responseInputStream.read(byteContainer)) != -1; ) {
    responseStringBuffer.append(new String(byteContainer, 0, i));
}

JSONObject response = new JSONObject(responseStringBuffer.toString());
我的JSON响应-

{
    "firstOne":"XXXXXXXXXX",
    "secOne":"XXXXXXXXXXXXXXXXXX",
    "thrOne":"XXXXXXXXXXXXX",
    "final":"XXXXXXXXXXXXXXX"
}
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
06-02 12:09:13.975 2310-3800/X.x W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
06-02 12:09:13.975 2310-3800/X.x W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:159)
06-02 12:09:13.975 2310-3800/X.x W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:172)
错误日志-

{
    "firstOne":"XXXXXXXXXX",
    "secOne":"XXXXXXXXXXXXXXXXXX",
    "thrOne":"XXXXXXXXXXXXX",
    "final":"XXXXXXXXXXXXXXX"
}
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
06-02 12:09:13.975 2310-3800/X.x W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
06-02 12:09:13.975 2310-3800/X.x W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:159)
06-02 12:09:13.975 2310-3800/X.x W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:172)
org.json.JSONException:Value
换成这个。

试试这个

            int responseCode=conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder sb = new StringBuilder();

                String json;
                while ((json = bufferedReader.readLine()) != null) {
                    sb.append(json + "\n");
                }
                return sb.toString().trim();
            }
您可以从sb.toString().trim()获取JSONObject。
祝你好运

你能发布错误日志吗?并在解析json的地方发布代码。您从服务器收到错误,可能是您的服务器没有从您的服务器端获取正确的参数,或者检查您的服务器端代码我会尝试一下sendParam是什么?是JSON吗?你打印了回复了吗?特别是您得到的响应是什么?Content-Length-entity-header字段指示实体体的大小,并且在您的回答中传递“application/json”。