android显示错误org.json.JSONException:java.lang.String类型的值<br不能转换为JSONObject

时间:2019-07-20 12:40:53

标签: android android-studio android-studio-3.0

我正在从事与Android相关的作业。我尝试了3次,并查看了各种方法来解决我面临的问题,但结果仍然相同。

此项目与android原生有关,但使用mysql和带有托管代码的php代码及其数据库。

当我完成编写的代码时,没有红色符号,但是在调试和测试logcat时,它会显示这样的错误消息

2019-07-20 19:28:26.776 1109-1109/? W/WindowManager: removeWindowToken: Attempted to remove non-existing token: android.os.Binder@eab9d7a
2019-07-20 19:28:26.975 14345-14345/com.halimlab.koneksicrud W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
2019-07-20 19:28:26.976 14345-14345/com.halimlab.koneksicrud W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)
2019-07-20 19:28:26.976 14345-14345/com.halimlab.koneksicrud W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:163)
2019-07-20 19:28:26.976 14345-14345/com.halimlab.koneksicrud W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:176)
2019-07-20 19:28:26.976 14345-14345/com.halimlab.koneksicrud W/System.err:     at com.halimlab.koneksicrud.TampilanSemuaBarang.tampilBarang(TampilanSemuaBarang.java:56)
2019-07-20 19:28:26.976 14345-14345/com.halimlab.koneksicrud W/System.err:     at com.halimlab.koneksicrud.TampilanSemuaBarang.access$100(TampilanSemuaBarang.java:26)
2019-07-20 19:28:26.976 14345-14345/com.halimlab.koneksicrud W/System.err:     at com.halimlab.koneksicrud.TampilanSemuaBarang$1GetJSON.onPostExecute(TampilanSemuaBarang.java:94)
2019-07-20 19:28:26.977 14345-14345/com.halimlab.koneksicrud W/System.err:     at com.halimlab.koneksicrud.TampilanSemuaBarang$1GetJSON.onPostExecute(TampilanSemuaBarang.java:80)
2019-07-20 19:28:26.977 14345-14345/com.halimlab.koneksicrud W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:695)
2019-07-20 19:28:26.977 14345-14345/com.halimlab.koneksicrud W/System.err:     at android.os.AsyncTask.access$600(AsyncTask.java:180)
2019-07-20 19:28:26.977 14345-14345/com.halimlab.koneksicrud W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
2019-07-20 19:28:26.977 14345-14345/com.halimlab.koneksicrud W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2019-07-20 19:28:26.977 14345-14345/com.halimlab.koneksicrud W/System.err:     at android.os.Looper.loop(Looper.java:193)
2019-07-20 19:28:26.978 14345-14345/com.halimlab.koneksicrud W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6739)
2019-07-20 19:28:26.978 14345-14345/com.halimlab.koneksicrud W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-07-20 19:28:26.978 14345-14345/com.halimlab.koneksicrud W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
2019-07-20 19:28:26.978 14345-14345/com.halimlab.koneksicrud W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:859)
2019-07-20 19:28:27.068 545-1023/? W/SurfaceFlinger: Attempting to set client state on removed layer: Mengambil Data#0
2019-07-20 19:28:27.068 545-1023/? W/SurfaceFlinger: Attempting to set client state on removed layer: Dim Layer for - Task=1791#0
2019-07-20 19:28:27.068 545-1023/? W/SurfaceFlinger: Attempting to destroy on removed layer: Mengambil Data#0
2019-07-20 19:28:27.068 545-1023/? W/SurfaceFlinger: Attempting to destroy on removed layer: Dim Layer for - Task=1791#0

错误消息指向以下功能中正确文件中的一行,我不太了解发生了什么,所以请帮助我

private void tampilBarang(){
        JSONObject jsonObject = null;
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);
            for(int i = 0; i<result.length(); i++){
                JSONObject jo = result.getJSONObject(i);
                String id = jo.getString(konfigurasi.TAG_ID);
                String name = jo.getString(konfigurasi.TAG_NAMA);

                HashMap<String,String> employees = new HashMap<>();
                employees.put(konfigurasi.TAG_ID,id);
                employees.put(konfigurasi.TAG_NAMA,name);
                list.add(employees);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ListAdapter adapter = new SimpleAdapter(
                TampilanSemuaBarang.this, list, R.layout.list_item,
                new String[]{konfigurasi.TAG_ID, konfigurasi.TAG_NAMA},
                new int[]{R.id.id, R.id.name}
        );
        listView.setAdapter(adapter);
    }

这是与错误相对应的文件的代码

<?php 
    //Import File Koneksi Database
    require_once('conn.php');

    //Membuat SQL Query
    $sql = "SELECT * FROM barang";

    //Mendapatkan Hasil
    $r = mysqli_query($con,$sql);

    //Membuat Array Kosong 
    $result = array();

    while($row = mysqli_fetch_array($r)){

        //Memasukkan Nama dan ID kedalam Array Kosong yang telah dibuat 
        array_push($result,array(
            "id"=>$row['id_barang'],
            "name"=>$row['nama_barang']
        ));
    }

    //Menampilkan Array dalam Format JSON
    echo json_encode(array('result'=>$result));

    mysqli_close($con);
?>

<?php 

   //Mendapatkan Nilai Dari Variable ID Pegawai yang ingin ditampilkan
   $id = $_GET['id_barang'];

   //Importing database
   require_once('conn.php');

   //Membuat SQL Query dengan pegawai yang ditentukan secara spesifik sesuai ID
   $sql = "SELECT * FROM barang WHERE id_barang=$id";

   //Mendapatkan Hasil 
   $r = mysqli_query($con, $sql);

   //Memasukkan Hasil Kedalam Array
   $result = array();
   $row = mysqli_fetch_array($r);
   array_push($result,array(
       "id_barang"=>$row['id_barang'],
       "nama_barang"=>$row['nama_barang'],
       "kode_barang"=>$row['kode_barang'],
       "stok_barang"=>$row['stok_barang']
       ));

   //Menampilkan dalam format JSON
   echo json_encode(array('result'=>$result));

   mysqli_close($con);
?>

我只是希望所有这些都可以轻松解决,谢谢

0 个答案:

没有答案