简体   繁体   English

Json无法在ListView中显示

[英]Json cannot display in ListView

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, data not came out in my app. 当我从该站点使用url源时,该应用程序可以完全运行,但是当我从数据库中使用本地数据json ,数据不会在我的应用程序中输出。

The in line 99 and 68 (you can see my command in code) 在第99和68行中(您可以在代码中看到我的命令)

This my code 这是我的代码

testing.php testing.php

<?php
    //Create Database connection
    $db = mysql_connect("localhost","root","root");
    if (!$db) {
        die('Could not connect to db: ' . mysql_error());
    }

    //Select the Database
    mysql_select_db("skripsi",$db);

    //Replace * in the query with the column names.
    $result = mysql_query("select * from maintenance", $db);  

    //Create an array
    $json_response = array( "maintenance" => array());

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $row_array['mid'] = $row['mid'];
        $row_array['pid'] = $row['pid'];
        $row_array['status'] = $row['status'];
        $row_array['head'] = $row['head'];
        $row_array['note'] = $row['note'];

        //push the values in the array
        array_push($json_response["maintenance"],$row_array);
    }
    echo json_encode($json_response);
?>

This my JSONParse class 这是我的JSONParse类

private class JSONParse extends AsyncTask<String, String, JSONObject> {  **//LOGCAT NAVIGATE TO THIS LINE**
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mid = (TextView) getActivity().findViewById(R.id.mid);
        pid = (TextView) getActivity().findViewById(R.id.pid);
        status = (TextView) getActivity().findViewById(R.id.status);
        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
            maintenance = json.getJSONArray(TAG_OS);   **//LOGCAT NAVIGATE TO THIS LINE**
            for(int i = 0; i < maintenance.length(); i++){
                JSONObject c = maintenance.getJSONObject(i);
                // Storing  JSON item in a Variable
                String mid = c.getString(TAG_MID);
                String pid = c.getString(TAG_PID);
                String status = c.getString(TAG_STATUS);
                String head = c.getString(TAG_HEAD);
                String note = c.getString(TAG_NOTE);
                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_MID, mid);
                map.put(TAG_PID, pid);
                map.put(TAG_STATUS, status);
                map.put(TAG_HEAD, head);
                map.put(TAG_NOTE, note);
                oslist.add(map);
                list=(ListView) getActivity().findViewById(R.id.list);
                ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                        R.layout.list_v,
                        new String[] { TAG_MID,TAG_PID, TAG_STATUS, TAG_HEAD, TAG_NOTE}, new int[] {
                        R.id.mid,R.id.pid, R.id.status,R.id.head, R.id.note});
                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;
}

logcat 日志猫

09-16 14:46:30.661    3481-3481/com.example.blackcustomzier.skripsi W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ org.json.JSONException: No value for android
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:354)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:548)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at com.example.blackcustomzier.skripsi.FragmentMaintain$JSONParse.onPostExecute(FragmentMaintain.java:99)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at com.example.blackcustomzier.skripsi.FragmentMaintain$JSONParse.onPostExecute(FragmentMaintain.java:68)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:631)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-16 14:46:30.791    3481-3481/com.example.blackcustomzier.skripsi W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

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

maintenance = json.getJSONArray(TAG_OS); 维护= json.getJSONArray(TAG_OS);

looks like value of TAG_OS is "android" but in your json object there is no array with the name "android" so first print the json inf the form of string format it using some foramtter and make sure the name of your json array is "android". 看起来TAG_OS的值是“ android”,但是在您的json对象中没有名称为“ android”的数组,因此首先使用一些格式将json inf以字符串格式的形式打印出来,并确保json数组的名称为“ android”。

try this link to parse your json http://jsonformatter.curiousconcept.com/ 试试这个链接来解析您的json http://jsonformatter.curiousconcept.com/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM