Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley POST string request unexpected error 500

I am using Volley library in my project and getting Unexpected response code 500 as response.

I have searched stackoverflow thoroughly and still unable to find solution that works.

Following is my code for making GET string request

        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)

Following is the stacktrace

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)

Following is PHP code that I wrote on server side:

<?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();
?>
like image 502
Muhammad Faizan Avatar asked Sep 03 '19 10:09

Muhammad Faizan


2 Answers

I have something First import the okhttp inside the Gradle dependency (Library). here is the documentation

https://square.github.io/okhttp/

After open the postman and click on the code menu enter image description here

As you can see below the send Button the code button is there. click it and select the java-> okhttp

copy the code and paste it inside android studio. it has 99.9 % chance it will work.

like image 104
raj kavadia Avatar answered Oct 06 '22 01:10

raj kavadia


I modified the code a bit and checked it in postman, it works fine.

I moved the JSON encode statment out of the if statement.

// 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();
like image 41
Syed Usama Ahmad Avatar answered Oct 05 '22 23:10

Syed Usama Ahmad