【问题标题】:Error parsing data org.json.JSONException: Value <!-- of type java.lang.String cannot be converted to JSONObject解析数据时出错 org.json.JSONException: Value <!-- of type java.lang.String cannot be convert to JSONObject
【发布时间】:2014-09-17 10:09:57
【问题描述】:

我正在尝试按照教程获取 jsonListView, 这个应用程序只在json 中显示Array。 当我使用来自this site 的 url 源时,该应用程序可以完全运行 但是当我使用数据库中的本地数据json 时,总是会出错。

错误是

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

并在第 99 行和第 68 行显示错误

这是我的代码

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>

这是我的 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();
        }
    }
}

这是我的 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;
}

希望有人能帮助我,谢谢

【问题讨论】:

  • 您确定要在该文件中包含 html 标签吗?我希望文件是test.json,里面只有一个有效的 json 对象
  • 错误清楚地表明您的 JSON 无效。在您的代码中使用之前,在浏览器插件中检查一次 JSON 的格式
  • 如果您也发布完整的 Logcat 将会很有帮助....

标签: java android html json


【解决方案1】:

您的“tes.html”(原文如此)源中不应包含任何 HTML 标记。它应该只是 JSON 文本,并且可能应该命名为“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" } ] }

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多