JSONParser突然出现错误异常没有值

时间:2014-11-12 09:27:25

标签: android mysql json

在我收到此错误之前,我更改了我的表格结构。 然后重新检查查询和PHP变量,但我不知道为什么它给了我错误。 所以我在php中调用我的函数,并成功生成JSON编码:

{
  "data": [{
  "id": "18",
  "jenis": "Kelas",
  "ruangan": "9120",
  "hari": "Selasa",
  "jam": "08:00:00"
  }],
"success": 1
}

我的logcat显示异常:没有jenis的值。

private JSONParser jParser;
private ArrayList<HashMap<String, String>> ListKuliah;
private static final String TAG_SUCCESS = "success";
private static final String TAG_DATA = "data";
private static final String TAG_NPM = "npm";
private static final String TAG_KODE = "kode";
private static final String TAG_JENIS = "jenis";
private static final String TAG_RUANGAN = "ruangan";
private static final String TAG_HARI = "hari";
private static final String TAG_JAM = "jam";
private static final String TAG_ID_JADWAL = "id";
private static final String TAG_JUMLAH = "jumlah";
private String npm,kode;
private JSONArray data = null;

class getMatkul extends AsyncTask<String,String,String> {
    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        String request = "getKelasByKode";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("request", request));
        params.add(new BasicNameValuePair("npm", npm));
        params.add(new BasicNameValuePair("kode", kode));
        JSONObject json = jParser.makeHttpRequest("GET", params);   // getting JSON string from URL
        Log.d("All kelas: ", json.toString());  // Check your log cat for JSON reponse

        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) { 
                data = json.getJSONArray(TAG_DATA);     // Getting Array of matkul
                for (int i = 0; i < data.length(); i++) {   // looping through All matkul
                    JSONObject arr = data.getJSONObject(i); // Storing each json item in variable
                    String jenis = arr.getString(TAG_JENIS);
                    String ruangan = arr.getString(TAG_RUANGAN);

                    HashMap<String, String> map = new HashMap<String, String>();    // creating new HashMap
                    map.put(TAG_JENIS, jenis);      // adding each child node to HashMap key => value
                    map.put(TAG_RUANGAN, ruangan);
                    ListKuliah.add(map);    // adding HashList to ArrayList
                }
            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(DetailMatkul.this, ListKuliah, R.layout.list_kuliah, new String[] {TAG_JENIS, TAG_RUANGAN, TAG_HARI, TAG_JUMLAH},
                        new int[] { R.id.jenis, R.id.ruangan, R.id.hari, R.id.jumlah});
                setListAdapter(adapter);    // updating listview
            }
        });
    }
}

更新logcat:

11-11 22:19:20.617: D/All kelas:(10879): {"data":[{"kode":"AIF401","sks":"4","nama":"Skripsi 1"}],"success":1}
11-11 22:19:20.617: W/System.err(10879): org.json.JSONException: No value for jenis
11-11 22:19:20.627: W/System.err(10879):    at org.json.JSONObject.get(JSONObject.java:355)
11-11 22:19:20.627: W/System.err(10879):    at org.json.JSONObject.getString(JSONObject.java:515)
11-11 22:19:20.627: W/System.err(10879):    at com.skripsi.sistemabsensi.DetailMatkul$getMatkul.doInBackground(DetailMatkul.java:104)
11-11 22:19:20.627: W/System.err(10879):    at com.skripsi.sistemabsensi.DetailMatkul$getMatkul.doInBackground(DetailMatkul.java:1)
11-11 22:19:20.627: W/System.err(10879):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-11 22:19:20.627: W/System.err(10879):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-11 22:19:20.627: W/System.err(10879):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-11 22:19:20.627: W/System.err(10879):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-11 22:19:20.627: W/System.err(10879):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-11 22:19:20.627: W/System.err(10879):    at java.lang.Thread.run(Thread.java:841)

更新:我找到了解决方案,所以几天前我将我的url变量替换为JSONParser类,这给了我错误。

1 个答案:

答案 0 :(得分:1)

从日志中可以清楚地看出,响应对象是

{
 "data": [
{
  "kode": "AIF401",
  "sks": "4",
  "nama": "Skripsi 1"
}
],
"success": 1
}

并且不等于您的预期回复

{
"data": [{
"id": "18",
"jenis": "Kelas",
"ruangan": "9120",
"hari": "Selasa",
"jam": "08:00:00"
}],
"success": 1
}

它不包含您指定的任何标签。检查 php 代码。