简体   繁体   English

解析数据org.json.JSONException时出错:值

[英]Error parsing data org.json.JSONException: Value <!— of type java.lang.String cannot be converted to JSONObject

I am trying to following a tutorial get json to ListView , this app just shows Array in json . 我正在尝试按照教程将json获取到ListView ,此应用仅显示json中的Array When I use the url source from this site , the app works completely but when I use local data json from my database, I always get an error. 当我从该站点使用url源时,该应用程序完全可以运行,但是当我从数据库中使用本地数据json ,总是会收到错误消息。

The error is 错误是

Error parsing data org.json.JSONException: Value <!-- of type java.lang.String 
cannot be converted to JSONObject

and show error in line 99 and 68 并在第99和68行显示错误

This my code 这是我的代码

tes.html tes.html

<html>
  <head></head>
  <body>
    { "android": [ { "ver": "1.5", "name": "Cupcake", "api": "API level 3" }, 
    { "ver": "1.6", "name": "Donut", "api": "API level 4" }, 
    { "ver": "2.0-2.1", "name": "Eclair", "api": "API level 5-7" }, 
    { "ver": "2.2", "name": "Froyo", "api": "API level 8" }, 
    { "ver": "2.3", "name": "Gingerbread", "api": "API level 9-10" }, 
    { "ver": "3.0-3.2", "name": "Honeycomb", "api": "API level 11-13" }, 
    { "ver": "4.0", "name": "Ice Cream Sandwich", "api": "API level 14-15" }, 
    { "ver": "4.1-4.3", "name": "JellyBean", "api": "API level 16-18" }, 
    { "ver": "4.4", "name": "KitKat", "api": "API level 19" } ] }
  <body>
</html>

This my JSONParse class 这是我的JSONParse类

private class JSONParse extends AsyncTask<String, String, JSONObject> {  // THIS LINE 68 ERROR
    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ver = (TextView) getActivity().findViewById(R.id.vers);
        name = (TextView) getActivity().findViewById(R.id.name);
        api = (TextView) getActivity().findViewById(R.id.api);
        //head = (TextView) getActivity().findViewById(R.id.head);
        //note = (TextView) getActivity().findViewById(R.id.note);
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.makeHttpRequestWithoutParams(url);
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            android = json.getJSONArray(TAG_OS); //THIS LINE 99 ERROR
            for(int i = 0; i < android.length(); i++) {
                JSONObject c = android.getJSONObject(i);
                // Storing  JSON item in a Variable
                String ver = c.getString(TAG_VER);
                String name = c.getString(TAG_NAME);
                String api = c.getString(TAG_API);
                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_VER, ver);
                map.put(TAG_NAME, name);
                map.put(TAG_API, api);
                oslist.add(map);
                list=(ListView) getActivity().findViewById(R.id.list);
                ListAdapter adapter = new SimpleAdapter(getActivity(),
                                              oslist,
                                              R.layout.list_v,
                                              new String[] { TAG_VER,TAG_NAME, TAG_API },
                                              new int[] { R.id.vers,R.id.name, R.id.api}
                                      );
                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(getActivity(), "You Clicked at " + oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

this my JSONObject 这是我的JSONObject

public JSONObject makeHttpRequestWithoutParams(String url) {
    // Making HTTP request
    try {
        // defaultHttpClient
        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 parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    // return JSON String
    return jObj;
}

hope someone can help me thanks 希望有人可以帮我谢谢

Your "tes.html" (sic) source should not have any HTML tags in it. 您的“ tes.html”(原文如此)源中不应包含任何HTML标记。 It should be JSON text only and probably should be named "test.json" instead (though the name of the file shouldn't really matter). 它应该仅是JSON文本,并且可能应该命名为“ test.json”(尽管文件名实际上并不重要)。

Proposed test.json: 建议的test.json:

{ "android": [ { "ver": "1.5", "name": "Cupcake", "api": "API level 3" }, 
{ "ver": "1.6", "name": "Donut", "api": "API level 4" }, 
{ "ver": "2.0-2.1", "name": "Eclair", "api": "API level 5-7" }, 
{ "ver": "2.2", "name": "Froyo", "api": "API level 8" }, 
{ "ver": "2.3", "name": "Gingerbread", "api": "API level 9-10" }, 
{ "ver": "3.0-3.2", "name": "Honeycomb", "api": "API level 11-13" }, 
{ "ver": "4.0", "name": "Ice Cream Sandwich", "api": "API level 14-15" }, 
{ "ver": "4.1-4.3", "name": "JellyBean", "api": "API level 16-18" }, 
{ "ver": "4.4", "name": "KitKat", "api": "API level 19" } ] }

暂无
暂无

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

相关问题 解析数据org.json.JSONException时出错:类型java.lang.String无法转换为JSONObject - Error parsing data org.json.JSONException: type java.lang.String cannot be converted to JSONObject 解析数据时出错 org.json.JSONException: Value<html> java.lang.String 类型无法转换为 JSONObject - Error parsing data org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject 解析数据org.json.JSONException时出错:类型为java.lang.String的值db_connect.php无法转换为JSONObject - Error parsing data org.json.JSONException: Value db_connect.php of type java.lang.String cannot be converted to JSONObject 从Pincode到State Error,解析数据org.json.JSONException:值 - From Pincode to State Error parsing data 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时出错:值HI。 类型java.lang.String的对象不能转换为JSONObject - Getting Error parsing data org.json.JSONException: Value HI. of type java.lang.String cannot be converted to JSONObject 解析数据org.json.JSONException时出错:值<br> - Error parsing data org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject 解析数据org.json.JSONException时出错:类型java.lang.String的值无法转换为JSONArray - Error Parsing Data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray org.json.JSONException:值类型为java.lang.String的数据不能转换为JSONObject - org.json.JSONException: Value Data of type java.lang.String cannot be converted to JSONObject 错误。 org.json.JSONException:值 - error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM