如何从PHP获取整个数组?

时间:2017-09-24 23:40:13

标签: php android

我有一个应用程序,它在ListView中显示数据库中的所有项目。但我有一个问题,我只得到数据库中的第一个元素。

这是我的PHP代码:

<?php

$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();
$dbh = $db->connect(); // here you get the connection

$query = "SELECT *FROM lost_cars";

$result = $dbh->prepare($query);
$result->execute();

if (!empty($result)) {
 // check for empty result
 if ($result->fetchAll() > 0) {


     foreach($dbh->query($query) as $row){
            $car["id"] = $row['id'];
            $car["name"] = $row['name'];

            $response["car"] = array();
            array_push($response["car"], $car);
        // echoing JSON response
        //$response["success"] = 1;
        echo json_encode($response);

        }

    } else {
        // no car found
        $response["success"] = 0;
        $response["message"] = "No car found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no car found
    $response["success"] = 0;
    $response["message"] = "No car found";

    // echo no users JSON
    echo json_encode($response);
}

&GT;

此代码正常运行并打印我之前加载的一些随机值。

  

{&#34;车&#34;:[{&#34; ID&#34;:&#34; 11&#34;&#34;名称&#34;:&#34; fgfg&#34; }]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 12&#34;&#34;名称&#34;:&#34; SDF&#34; }]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 14&#34;&#34;名称&#34;:&#34;克莱奥&#34; }]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 15&#34;&#34;名称&#34;:&#34;洛拉&#34; }]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 16&#34;&#34;名称&#34;:&#34; HOLA&#34; }]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 17&#34;&#34;名称&#34;:&#34; kjlhljh&#34; }]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 18&#34;&#34;名称&#34;:&#34; sdfgsdg&#34; }]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 19&#34;&#34;名称&#34;:&#34; JKL;&# 34;}]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 20&#34;&#34;名称&#34;:&#34; ASD&# 34;}]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 22&#34;&#34;名称&#34;:&#34; ASD&# 34;}]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 23&#34;&#34;名称&#34;:&#34; sdfsdfd&# 34;}]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 24&#34;&#34;名称&#34;:&#34; SDF&# 34;}]} {&#34;车&#34;:[{&#34; ID&#34;:&#34; 25&#34;&#34;名称&#34;:&#34; kgkgkg&# 34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 26&#34;&#34;名称&#34;:&#34; dfgdg&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 28&#34;&#34;名称&#34;:&#34; SDF&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 29&#34;&#34;名称&#34;:&#34; dfsdf&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 30&#34;&#34;名称&#34;:&#34; dfgdfg&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 31&#34;&#34;名称&#34;:&#34; sadasd&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 32&#34;&#34;名称&#34;:&#34; ACA&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 34&#34;&#34;名称&#34;:&#34; sdfsdf&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 35&#34;&#34;名称&#34;:&#34; sdfsdf&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 36&#34;&#34;名称&#34;:&#34; HOLA&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 37&#34;&#34;名称&#34;:&#34; qweqwe&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 38&#34;&#34;名称&#34;:&#34; DF&#34;}]} {&#34;车&#34 ;: [{&#34; ID&#34;:&#34; 39&#34;&#34;名称&#34;:&#34; SDA&#34;}]}

然后我成功解析了android中的结果,这里是JSONParser的一个片段:

 try {
            BufferedReader reader = new BufferedReader(new 
 InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            jObj = new JSONObject(json); //THE PROBLEM

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + 
 e.toString());
        }

问题是当我尝试将String(json)转换为JSONObject时。当我被传递给listView以显示我只获得第一个元素。

以下是ListViewActivity的代码片段:

protected String doInBackground(String... args) {
        // Building Parameters
        HashMap<String, String> params = new HashMap<>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(URLs.GET_ALL_CARS, "GET", 
 params);
        // Check your log cat for JSON reponse
        Log.d("All Cars: ", json.toString());

        try {
            // Checking for SUCCESS TAG
           // int success = json.getInt(TAG_SUCCESS);

            //if (success == 1) {
                // cars found
                // Getting Array of Products

                cars = json.getJSONArray(TAG_CARS);

                // looping through All Products
                for (int i = 0; i < cars.length(); i++) {
                    JSONObject c = cars.getJSONObject(i);
                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    System.out.println(id);
                    String name = c.getString(TAG_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, 
                    String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    // adding HashList to ArrayList
                    carList.add(map);
                }
            //}
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {

      // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllCarsActivity.this, carList,
                        R.layout.view_item, new String[]{TAG_ID,
                        TAG_NAME},
                        new int[]{R.id.id, R.id.
                setListAdapter(adapter);
            }
        });



    }

这可能是什么问题?我已经谷歌搜索了几个小时没有成功,我不知道该怎么做。 非常感谢你的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

尝试用json_decode解码json,然后遍历你的数组数组,也许是嵌套的foreach循环?对不起,我自己还能提供更多帮助