Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分析数据org.json.JSONException时出错:值<;!--无法将java.lang.String类型的转换为JSONObject_Java_Android_Html_Json - Fatal编程技术网

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

分析数据org.json.JSONException时出错:值<;!--无法将java.lang.String类型的转换为JSONObject,java,android,html,json,Java,Android,Html,Json,我正在尝试按照教程获取json到ListView, 此应用程序仅在json中显示Array。 当我使用来自的url源时,应用程序完全可以工作 但是当我使用数据库中的本地数据json时,总是会出现错误 错误是 Error parsing data org.json.JSONException: Value <!-- of type java.lang.String cannot be converted to JSONObject 希望有人能帮助我,谢谢您的“tes.html”(sic)

我正在尝试按照教程获取
json
ListView
, 此应用程序仅在
json
中显示
Array
。 当我使用来自的url源时,应用程序完全可以工作 但是当我使用数据库中的本地数据
json
时,总是会出现错误

错误是

Error parsing data org.json.JSONException: Value <!-- of type java.lang.String 
cannot be converted to JSONObject
希望有人能帮助我,谢谢

您的“tes.html”(sic)源代码中不应该有任何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" } ] }

您确定要在该文件中包含html标记吗?我希望该文件是
test.json
,里面只有一个有效的json对象。错误清楚地表明您的json无效。在代码中使用JSON之前,请在浏览器插件中检查JSON的格式。如果您也发布完整的日志,这将非常有用。。。。
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();
        }
    }
}
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;
}
{ "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" } ] }