我在项目中使用Volley库,并获得Unexpected response code 500作为响应。

我已经彻底搜索了stackoverflow,但仍然找不到有效的解决方案。

以下是我发出GET字符串请求的代码

        val API = "http://squadtechsolution.com/android/v1/allcompany.php"
        val requestQueue = Volley.newRequestQueue(mActivity)
        val stringRequest = StringRequest(
            Request.Method.GET,
            API,
            Response.Listener { response ->
                Log.i("dxdiag", response)
                mView.onFetchHttpDataResult(true)
                Toast.makeText(context, response, Toast.LENGTH_LONG).show()
            },
            Response.ErrorListener { error ->
                Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show()
                Log.i("dxdiag", error.printStackTrace().toString())
                mView.onFetchHttpDataResult(false)
            })
        requestQueue.add(stringRequest)

以下是堆栈跟踪
2019-09-03 17:15:53.237 3308-3892/com.squadtechs.markhor.foodapp
E/Volley: [194] BasicNetwork.performRequest: Unexpected response code
500 for
http://squadtechsolution.com/android/v1/allcompany.php
2019-09-03 17:15:53.243 3308-3351/com.squadtechs.markhor.foodapp
D/EGL_emulation: eglMakeCurrent: 0xa7d84180: ver 2 0 (tinfo
0xa7d832b0)
2019-09-03 17:15:53.256 3308-3308/com.squadtechs.markhor.foodapp
W/System.err: com.android.volley.ServerError
2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp
W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:205)2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)

以下是我在服务器端编写的PHP代码:

<?php
    require 'db.php';

    $sql = "SELECT * FROM `company_profile`";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        $id=$row['id'];

        $company_name=$row['company_name'];
        $cuisine=$row['cuisine'];
        $conpany_phone=$row['conpany_phone'];
        $company_description=$row['company_description'];
        $company_logo=$row['company_logo'];
        $company_type=$row['company_type'];
        $delivery_type=$row['delivery_type'];
        $delivery_range=$row['delivery_range'];
        $delivery_fee=$row['delivery_fee'];
        $delivery_pickupinfo=$row['delivery_pickupinfo'];
        $address=$row['address'];

        $companyData[] = array('id'=> $id,'company_name'=>
        $company_name,'cuisine'=> $cuisine,'conpany_phone'=>
        $conpany_phone,'company_description'=>
        $company_description,'company_logo'=> $company_logo,'company_type'=>
        $company_type,'delivery_type'=> $delivery_type,'delivery_range'=>
        $delivery_range,'delivery_fee'=>
        $delivery_fee,'delivery_pickupinfo'=> $delivery_pickupinfo,'address'=> $address);
    }
    echo $jsonformat=json_encode($companyData);
    } else {
        echo "0 results";
    }
    $conn->close();
?>

最佳答案

我修改了一下代码,并在邮递员中对其进行了检查,效果很好。

我将JSON编码语句从if语句中移出。

// output data of each row
while($row = $result->fetch_assoc()) {

    $id=$row['id'];

    $company_name=$row['company_name'];
    $cuisine=$row['cuisine'];
    $conpany_phone=$row['conpany_phone'];
    $company_description=$row['company_description'];
    $company_logo=$row['company_logo'];
    $company_type=$row['company_type'];
    $delivery_type=$row['delivery_type'];
    $delivery_range=$row['delivery_range'];
    $delivery_fee=$row['delivery_fee'];
    $delivery_pickupinfo=$row['delivery_pickupinfo'];
    $address=$row['address'];

    $companyData[] = array('id'=> $id,'company_name'=>
    $company_name,'cuisine'=> $cuisine,'conpany_phone'=>
    $conpany_phone,'company_description'=>
    $company_description,'company_logo'=> $company_logo,'company_type'=>
    $company_type,'delivery_type'=> $delivery_type,'delivery_range'=>
    $delivery_range,'delivery_fee'=>
    $delivery_fee,'delivery_pickupinfo'=> $delivery_pickupinfo,'address'=> $address);
}
echo $jsonformat=json_encode($companyData);

$conn->close();

08-05 18:36