3

I am using volley library for network calls in Android and jsonObject for sending and getting values from the server.

In my case if I send the correct value, it returns the correct json object, but if I send a wrong input to the server it returns 401 as a request header and response body with error message . But that's not showing in onResponse success method.

here is my code

private void makeLoginReq() {
    showProgressDialog();
    Map<String, String> postParam = new HashMap<String, String>();
    postParam.put("un", user_name.getText().toString());
    postParam.put("p", password.getText().toString());

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Server.URL+Server.FUNC_LOGIN, new JSONObject(postParam),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAG", response.toString());
                    hideProgressDialog();
                    try {
                        if("0".equalsIgnoreCase(response.getString("status")))
                        {
                            startActivity(new Intent(ActivityLogin.this,ActivityHome.class));
                        }else if("1".equalsIgnoreCase(response.getString("status")))
                        {

                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("TAG", "Error: " + error.networkResponse.statusCode);
                    hideProgressDialog();
                    Crouton.makeText(ActivityLogin.this, "Server Error Try again", Style.ALERT).show();
                }
            }) {

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

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}

My need is to get the json response in public void onResponse(JSONObject response) { method }.

Can anyone help me to overcome this problem?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
John
  • 1,407
  • 7
  • 26
  • 51

1 Answers1

-1

There are a lot sub classes of VolleyError, you can check the error type with instanceOf checking.

cn123h
  • 2,302
  • 1
  • 21
  • 16
  • the return value from server is JSON format only. but y i am not receiving in Onsuccess method – John Feb 06 '15 at 11:01
  • but you changed the response header, i think you will get the response in onErrorResponse – cn123h Feb 06 '15 at 11:02
  • great and thanks you are right . but is this possible to get the JSONObject from Error : i mean how can i get the response body from onResponse Error method – John Feb 06 '15 at 11:08
  • @John check this answer: http://stackoverflow.com/questions/21867929/android-how-handle-message-error-from-the-server-using-volley – cn123h Feb 06 '15 at 11:12