sum20156
sum20156

Reputation: 736

Volley: BasicNetwork.performRequest: Unexpected response code 500

I trying to send a post request to my server via volley library. But it is not working. I have tested my API in Postman and it is working there. I have used the same parameter.

The error I am getting:

BasicNetwork.performRequest: Unexpected response code 500 for 
https://demo.homexpress.in/api/make_an_order
2020-06-23 08:57:46.163 9351-9351/com.apitechno.grocery W/System.err: com.android.volley.ServerError
2020-06-23 08:57:46.164 9351-9351/com.apitechno.grocery W/System.err:     at 
com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:205)
2020-06-23 08:57:46.164 9351-9351/com.apitechno.grocery W/System.err:     at 
com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
2020-06-23 08:57:46.164 9351-9351/com.apitechno.grocery W/System.err:     at 
com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
2020-06-23 08:57:46.164 9351-9351/com.apitechno.grocery W/System.err:     at 
com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)

This is the function where I am sending request:

  private void continueUrl(final String todaydate,final String timeslot,final ArrayList<HashMap<String, String>> map) {
    StringRequest stringRequest = new StringRequest(Request.Method.POST, OrderContinue, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("ordermake",response);
            try {
                JSONObject jsonObject = new JSONObject(response);
                String status = jsonObject.getString("status");
                String msg = jsonObject.getString("message");
                if (status.equals("1")){

                    JSONObject object = jsonObject.getJSONObject("data");

                    cart_id = object.getString("cart_id");
                    Intent intent=new Intent(getApplicationContext(),PaymentDetails.class);

                    intent.putExtra("order_amt",txt_totalPrice.getText().toString());
                    startActivity(intent);
                }


                else {
                    Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
                }
                progressDialog.dismiss();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            progressDialog.dismiss(); }


    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    })

    {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String,String> headers = new HashMap<>();
            headers.put("Content-Type","application/json");
            return headers;
        }
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String,String> param = new HashMap<>();
            param.put("time_slot",timeslot);
            param.put("address_id",ad_id);
            param.put("user_id",user_id);
            param.put("delivery_date",todaydatee);
            param.put("delivery_charge",DeliveryCharge.getText().toString());

            param.put("order_array", array.toString());
                Log.d("tag", param.toString());
            return param;
        }


    };
    RequestQueue requestQueue = Volley.newRequestQueue(OrderSummary.this);
    requestQueue.add(stringRequest);

}

Log.d("tag", param.toString()); print this:

{delivery_date=2020-06-23, delivery_charge=0, user_id=185, order_array= 
[{"qty":"1","varient_id":"55","product_image":"images\/product\/16-06-2020\/good.jpg"}], 
address_id=8, time_slot=13:00 - 16:00}

Here is a snapshot of postman:

enter image description here

Upvotes: 3

Views: 2852

Answers (1)

sum20156
sum20156

Reputation: 736

well, I have wasted almost a day then I found the solution.

If you face the same problem do one thing:

  @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String,String> headers = new HashMap<>(); 

        /*change content-type to "application/x-www-form-urlencoded" from 
         "application/json"
         */

        headers.put("Content-Type","application/x-www-form-urlencoded");
        return headers;
    }

Upvotes: 2

Related Questions