JSONException:无法将类型为java.lang的值<!DOCTYPE转换为JSONObject [英] JSONException: Value <!DOCTYPE of type java.lang cannot be converted to JSONObject

查看:136
本文介绍了JSONException:无法将类型为java.lang的值<!DOCTYPE转换为JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Guys can you help me a little bit, Im getting this error:

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

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

When I'm parsing the data here is my code:

    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;

    }

}

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

Here is the code where I'm instantiating the 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);
}

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

I'm really stuck with this. I'm populating the spinner from a database in a backend on Django in the server.

这是我的JSON数据

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

推荐答案

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

This issue comes from the server.
The URL you're requesting, send you back data but not in the JSON format.
The Exception you get is telling you that the String the server send you back starts with:

<!DOCTYPE 

这可以是:

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

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

To debug this further, simply print the content of your json variable in the logcat:

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

这篇关于JSONException:无法将类型为java.lang的值&lt;!DOCTYPE转换为JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆