JSONArray给出Null指针异常

时间:2014-03-05 18:07:53

标签: php android json nullpointerexception arrays

我从服务器获取数据,并希望在andriod app中的列表中显示此数据。

我正在获取json对象,但是当我打算将json对象转换为json数组时,它会给我一个NullPointerException,如下面的日志所示。我该如何解决这个问题?

我的源代码:

@Override
protected String doInBackground(String... arg0) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    JSONObject jsonObject= new JSONParser().makeHttpRequest(URL, "GET", params);
    try {
        jsonArray = jsonObject.getJSONArray("products");
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
    Log.d("All entries","Entries" + jsonObject.toString());

    try{
        Log.d("Seen","seen2");
        int success = jsonObject.getInt(TAG_SUCCESS);
        Log.d("Seen","seen5 " + success);
        if(success == 1){
            //Entries found
            Log.d("Seen","seen6");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject temp = jsonArray.getJSONObject(i);

                //String id = temp.getString(TAG_PID);
                Log.d("Seen","seen6");
                //name = temp.getString("name");
                //longitude = temp.getString("longitude");
                name = temp.getString(TAG_NAME);
                phone = temp.getInt(TAG_PHONE);
                ph = Integer.toString(phone);
                Log.d(TAG_NAME, ph);
                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_PHONE, ph);

                // Place name
                map.put(TAG_NAME, name);
                placesListItems.add(map);
            }
            bool = true;

        }else if(success== 0){
            Log.d("success1","success hogyi");
            bool = false;
        }
    } catch (Exception e) {
        Log.e("success","success status " + e);
    }
    return null;
}

protected void onPostExecute(String file_url) {
    // dismiss the dialog after getting all products
    pDialog.dismiss();
    // updating UI from Background Thread
    runOnUiThread(new Runnable() {

        public void run() {
           /**
            * Updating parsed Places into LISTVIEW
            * */

            ListAdapter adapter = new SimpleAdapter(
                Displayleads.this, placesListItems,
                R.layout.list, new String[] {
                    TAG_PHONE   , TAG_NAME },
                new int[] { R.id.phone, R.id.name });

            // Adding data into listview
            lv.setAdapter(adapter);
        }});}
    }}

PHP代码:

<?php

/*
 * Following code will list all the products
 */
include("db_connect.php");
// array for JSON response
$response = array();


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

// connecting to db
$db = new DB_CONNECT();
$c= $db->connect();
// get all products from products table
$result = mysqli_query($c,"SELECT * FROM crm") or die(mysqli_error());

// check for empty result
if (mysqli_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["products"] = array();

    while ($row = mysqli_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["name"] = $row["name"];
        $product["phone"] = $row["phone"];
        $product["email"] = $row["email"];
        $product["desgn"] = $row["desgn"];
        $product["leads"] = $row["leads"];
       // $product["updated_at"] = $row["updated_at"];



        // push single product into final response array
        array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;

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

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

堆栈追踪:

03-05 22:50:11.410: D/Hope(31826): Hope 5
03-05 22:50:11.410: D/Hope(31826): Hope 6
03-05 22:50:11.410: D/Hope(31826): Hope 7
03-05 22:50:11.440: D/Hope(31826): Hope 8
03-05 22:50:11.440: D/eee(31826): json{"products":[{"name":"gg","phone":"44","email":"ff","desgn":"ff","leads":"fs"},{"name":"djaj","phone":"3828","email":"fjau","desgn":"ahsy","leads":"qqq"},{"name":"arj","phone":"122","email":"css","desgn":"add","leads":"qqw"},{"name":"ass","phone":"0","email":"ff","desgn":"aaa","leads":"qqq"},{"name":"aj","phone":"1234","email":"ff","desgn":"software","leads":"a"}],"success":1}<br />
03-05 22:50:11.440: D/eee(31826): <b>Warning</b>:  mysqli_close() expects exactly 1 parameter, 0 given in <b>/home3/wta/public_html/amiranzur.com/android_connect/db_connect.php</b> on line <b>38</b><br />
03-05 22:50:11.450: D/All entries(31826): Entries{"success":1,"products":[{"leads":"fs","phone":"44","desgn":"ff","email":"ff","name":"gg"},{"leads":"qqq","phone":"3828","desgn":"ahsy","email":"fjau","name":"djaj"},{"leads":"qqw","phone":"122","desgn":"add","email":"css","name":"arj"},{"leads":"qqq","phone":"0","desgn":"aaa","email":"ff","name":"ass"},{"leads":"a","phone":"1234","desgn":"software","email":"ff","name":"aj"}]}
03-05 22:50:11.450: D/Seen(31826): seen2
03-05 22:50:11.450: D/Seen(31826): seen5 1
03-05 22:50:11.450: D/Seen(31826): seen6
03-05 22:50:11.450: E/success(31826): success status java.lang.NullPointerException
03-05 23:00:05.459: D/dalvikvm(32024): GC_EXPLICIT freed 87K, 47% free 2879K/5379K, external 1824K/2129K, paused 32ms

0 个答案:

没有答案