JSONException:值<!DOCTYPE类型java.lang无法转换为JSONObject

Dan*_*aja 1 java android json

伙计们,你可以帮助我一点,我得到这个错误:

"JSONException: Value <!DOCTYPE of type java.lang cannot be converted to JSONObject"
Run Code Online (Sandbox Code Playgroud)

当我在解析数据时,这是我的代码:

    public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    public JSONParser() {

    }
    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(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();

        }catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

}
Run Code Online (Sandbox Code Playgroud)

这是我实例化Parser的代码:

private void fillSpinnerCabTypes() {        
    List<String> cabTypesSpinner = new ArrayList<String>();
    JSONParser jsonParser = new JSONParser();
    JSONObject cabTypesObject = jsonParser.getJSONFromUrl(urlTypeCabs);
    try{
        TypesArray = cabTypesObject.getJSONArray(TAG_TYPES);

        for(int i = 0; i < TypesArray.length(); i++){
            JSONObject c = TypesArray.getJSONObject(i);
            String name = c.getString(TAG_NAME);
            cabTypesSpinner.add(name);
        }
    }catch(Exception e ){
        e.printStackTrace();
    }       

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.simple_spinner_item, cabTypesSpinner);

     final Spinner spnCabTypes = (Spinner)findViewById(R.id.spnTypeOfCab);
     adapter.setDropDownViewResource(
                            android.R.layout.simple_spinner_dropdown_item);
     spnCabTypes.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)

我真的很困惑.我正在服务器中的Django后端数据库中填充微调器.

这是我的JSON数据

{"Types": [{"name": "Normal"}, {"name": "Discapacitados"}, {"name": "Buseta"}]}
Run Code Online (Sandbox Code Playgroud)

Sim*_*uis 6

此问题来自服务器.
您请求的URL,向您发送数据,但不是JSON格式.
你得到的例外情况是告诉你服务器发送给你的字符串以:

<!DOCTYPE 
Run Code Online (Sandbox Code Playgroud)

这可以是:

  1. 一个简单的网页(而不是原始的JSON).它对应于网页的第一个XML标记()
  2. 服务器生成的错误页面,以HTML格式打印

要进一步调试,只需json在logcat中打印变量的内容:

Log.d("Debug", json.toString());
jObj = new JSONObject(json);
Run Code Online (Sandbox Code Playgroud)