解析数据时出错org.json.JSONException:类型java.lang.String的值<! - 无法转换为JSONObject - >

时间:2014-09-17 01:45:34

标签: java android html json

我正在尝试按照教程获取jsonListView, 此应用只会在Array中显示json。 当我使用来自this site的网址来源时,该应用程序可以正常运行 但是当我从我的数据库中使用本地数据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;
}

希望有人能帮助我,谢谢

1 个答案:

答案 0 :(得分:2)

您的&#34; tes.html&#34; (原文如此)source中不应包含任何HTML标记。它应该只是JSON文本,可能应该命名为&#34; test.json&#34;相反(尽管文件的名称并不重要)。

建议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" } ] }