Android volley:org.json.JSONException:(使用PDO)

时间:2016-03-03 05:24:51

标签: php android pdo android-volley

请原谅我可能的重复,我已经尝试了所有与我的情况相关的方式,但仍然没有得到任何结果。

我在这种情况下尝试使用PDO从MySql显示数据。

这是我的java回复:

  progressDialog.setMessage("Loading data..");
                RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
                JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL_GET_DATA, null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                progressDialog.dismiss();
                                Log.d("TAG", "EXE : " + response.optString("nama"));
                                try {
                                    JSONArray jsonArray = response.getJSONArray("identitas");
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                                        Toast.makeText(MainActivity.this, jsonObject.optString("nama"), Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(MainActivity.this, "ERROR : " + error.getMessage(), Toast.LENGTH_SHORT).show();
                                progressDialog.dismiss();
                                Log.d("TAG", "ERROR : " + error.getMessage());
                                System.out.println("ERROR : " + error);
                            }
                        });
                requestQueue.add(request);

我的PHP代码获取所有数据:

include "Connection.php";

class DisplayData {

    function getAllData(){
        $connection = new Connection();
        $conn = $connection->getConnection();

        $response = array();
        $response["identitas"] = array();

        try{
            $sql = "SELECT * FROM identitas";
                    $getData = $conn->prepare($sql);
                    $getData->execute();
                    if( $column = $getData->fetchColumn()>0){//for count row

                        $result = $getData->fetchAll(PDO::FETCH_ASSOC);
                        foreach($result as $data) {
                            echo $data["nama"] . '<br/>';
                            echo $data["umur"] . '<br/>';
                            echo $data["jeniskel"] . '<br/>';

                            //--------------------display json
                            //temp user array
                            $identitas = array();
                            $identitas["nama"] = $data["nama"];
                            $identitas["umur"] = $data["umur"];
                            $identitas["jeniskel"] = $data["jeniskel"];
                            array_push($response["identitas"], $identitas);
                        }
                        $response["success"] = 1;
                        $response["message"] = "Success";
                        echo json_encode($response);
                    }else{
                        $response["success"] = 1;
                        $response["message"] = "No Product Found";
                        echo json_encode($response);
                    }
        }catch (PDOException $e){
            echo "Failed to display " . $e->getMessage();
        }
    }
}

json响应应该是这样的:

{  
   "identitas":[  
      {  
         "nama":"Kacang tanah jagoan",
         "umur":"16 tahun",
         "jeniskel":"Tumbuhan bencong"
      },
      {  
         "nama":"Kacang tanah jagoan",
         "umur":"16 tahun",
         "jeniskel":"Tumbuhan bencong"
      },
      {  
         "nama":"kacang",
         "umur":"19 tahun",
         "jeniskel":"laki laki"
      },
      {  
         "nama":"Beruang Kutub",
         "umur":"200 tahun",
         "jeniskel":"Jantan Kewanitaan"
      },
      {  
         "nama":"Arak",
         "umur":"44",
         "jeniskel":""
      },
      {  
         "nama":"nangka",
         "umur":"22",
         "jeniskel":"buah"
      },
      {  
         "nama":"jonat",
         "umur":"18",
         "jeniskel":"laki laki"
      },
      {  
         "nama":"jonat",
         "umur":"18",
         "jeniskel":"laki laki"
      },
      {  
         "nama":"polongs",
         "umur":"12",
         "jeniskel":"laki laki"
      },
      {  
         "nama":"Jas",
         "umur":"30",
         "jeniskel":"Musik"
      },
      {  
         "nama":"jaran goyang",
         "umur":"12",
         "jeniskel":"eeee"
      },
      {  
         "nama":"dd",
         "umur":"33",
         "jeniskel":"fddf"
      },
      {  
         "nama":"kavang",
         "umur":"89",
         "jeniskel":"ikk"
      },
      {  
         "nama":"jangar",
         "umur":"1",
         "jeniskel":"gfff"
      }
   ],
   "success":1,
   "message":"Success"
}

0 个答案:

没有答案