0

the JSONObject is this:

{"tag":"login","success":1,"error":0,"name":"pb","dir":"DH","br":"LL","gr":"IW","email":"empty"}

logcat says:

 Value Access of type java.lang.String cannot be converted to JSONObject

the running an AsyncTask beside, and it works. why the volley-code says its a string? The answer from the server is a JSONObject for sure.... Why it says its a String?

private void makeJsonObjectRequest() {


        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
                url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());

                        try {
                            String dir = response.getString("dir");
                            Log.d("dir", dir);

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.d("JSONException", e.getMessage().toString());
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("onErrorResponse", error.getMessage().toString());
                    }
                })
        {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("tag", "login");
                params.put("name", name);
                params.put("password", password);
                return params;

            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                params.put("Content-Type","application/x-www-form-urlencoded");
                return params;
            }
            };
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }
Piet
  • 395
  • 2
  • 7
  • 17

2 Answers2

1

I don't know why it's append but I got the same issue with volley with the same type of request (HTTP POST + HEADER + PARAMS)

I fix it using StringRequest and parsing it manually. Try in this way

    StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {

            Log.d(TAG, "Success "+ s.toString());

            try {
                JSONObject data = new JSONObject(s);
                String dir = data.getString("dir");
                Log.d("dir", dir);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Error response " + error.getMessage());
        }
    }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>(); 
            params.put("tag", "login");
            params.put("name", name);
            params.put("password", password);
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            return params;
        }
    };
Ivan
  • 978
  • 6
  • 13
0

While the workaround with the StringRequest works fine, I found the "real" problem which is a very unintuitive default implementation in Volley which causes the POST request to turn into a GET. Check out my answer here for a solution.

Community
  • 1
  • 1
mattu
  • 944
  • 11
  • 24