Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley Server Error with null Network response

Every time I try to use POST method with Volley, I get sever error. I get null value in getCause, and some default value in getNetworkResponse.toString().

If I use GET method, this works fine (I get response from my url).

Can anybody help what can I do?

    Map<String, String> jsonParams = new HashMap<String, String>();
    jsonParams.put("teststr", "abd");

    RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
    JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.POST,
            url,
            new JSONObject(jsonParams),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        Toast.makeText(getApplicationContext(), "Success"+response.toString(), Toast.LENGTH_LONG).show();
                    }catch(Exception e){
                        Toast.makeText(getApplicationContext(), "JSON ERROR", Toast.LENGTH_LONG).show();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("abd", "Error: " + error
                                    + ">>" + error.networkResponse.statusCode
                                    + ">>" + error.networkResponse.data
                                    + ">>" + error.getCause()
                                    + ">>" + error.getMessage());
                }
            }) {

                @Override
                protected Map<String,String> getParams() {
                    HashMap<String, String> params = new HashMap<String, String>();
                    params.put("key", "value");
                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Content-Type", "application/json; charset=utf-8");
                    return headers;
                }
    };
    requestQueue.add(request);

Error Log:

Error: Error: com.android.volley.ServerError>>404>>[B@42b1e0d0>>null>>null

UPDATE: networkResponse.statusCode comes as 404, though the url is accessible (and return data if I just use GET method). If I remove header part in POST method, still the same.

the url:

<?php
    $response = array();

    $jsonString = file_get_contents('php://input');
    $jsonObj = json_decode($jsonString, true);

    if(!isset($jsonObj['teststr'])){
        $response["msg"] = "No data.";
    }else{
        $response["msg"] = "Success: ".$jsonObj['teststr'];
    }
    echo json_encode($response);
?>
like image 827
abdfahim Avatar asked May 14 '15 01:05

abdfahim


People also ask

How do I get volley response error?

If you want to view the data in the case of an error code in response, you can use something like this: @Override public void onErrorResponse(VolleyError error) { String body; //get status code here String statusCode = String. valueOf(error. networkResponse.

How does volley handle 401 error in Android?

check first if the error is an instance of NoConnectionError or any other error type which does not contain a networkResponse. In the other case, networkResponse should be set and also should contain the expected data.

What is Android volley ClientError?

It means the server returned a 4xx error code. volley/src/main/java/com/android/volley/toolbox/BasicNetwork.java. Line 199 in d1a3d53. throw new ClientError(networkResponse); All reactions.


2 Answers

problem is your Queue. change your volley code to this:

 RequestQueue queue = Volley.newRequestQueue(this);
 String URL = EndPoints.BASE_URL + "/call";
 StringRequest request = new StringRequest(Request.Method.POST, URL,
  new Response.Listener<String>()
  {
    @Override
    public void onResponse(String response) {

      Log.d("onResponse", response);

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

      NetworkResponse response = error.networkResponse;
      String errorMsg = "";
      if(response != null && response.data != null){
        String errorString = new String(response.data);
        Log.i("log error", errorString);
      }
    }
  }
) {
  @Override
  protected Map<String, String> getParams()
  {

    Map<String, String> params = new HashMap<String, String>();
    params.put("key_1","value_1");
    params.put("key_2", "value_2");
    Log.i("sending ", params.toString());

    return params;
  }

};


 // Add the realibility on the connection.
 request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f));

  // Start the request immediately
    queue.add(request);  

and your php (laravel) code to this:

    $response['success'] = true;
    $response['user']['tell'] = $user->tell;
    $response['user']['code'] = $user->code;
    $response['user']['time'] = $time;
    $response['user']['register_state'] = '1'
    return response()->json($response, 200);
like image 153
hadi.ghasemian Avatar answered Nov 15 '22 17:11

hadi.ghasemian


First, try to make sure your server works well. You can use Postman(chrome plug-in) or any other way to send a post request to the url and see what it responses.

After make sure there's no problem with your server, let us solve the problem with volley.

There's some problem with JsonObjectRequest when you use POST method. like this Volley JsonObjectRequest Post request not working.

I suggest you use StringRequest first and overwrite the getParams method like you did before. After you survive this task, you can try to write your own request, not very difficult but very useful.

I also suggest add request.setShouldCache(false) before requestQueue.add(request);. By default, volley saves the response in its cache and this behavior may cause some strange problem.

like image 22
Kay Wu Avatar answered Nov 15 '22 19:11

Kay Wu