伙计们,您能帮我一下吗,我收到此错误消息:

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


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

    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;

    }

}


这是我实例化解析器的代码:

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);
}


我真的很坚持。我正在从服务器上Django后端的数据库中填充微调框。

这是我的JSON数据

{"Types": [{"name": "Normal"}, {"name": "Discapacitados"}, {"name": "Buseta"}]}

最佳答案

此问题来自服务器。
您请求的URL向您发送回数据,但不是JSON格式。
您收到的异常告诉您服务器发回给您的String开头为:

<!DOCTYPE


可以是:


一个简单的网页(而不是原始JSON)。它对应于网页的第一个XML标签(source
服务器生成并以HTML打印的错误页面


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

Log.d("Debug", json.toString());
jObj = new JSONObject(json);

关于java - JSONException:类型java.lang的值<!DOCTYPE无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19872822/

10-09 19:47