Search code examples
androidjsonjsonobjectrequest

JsonObjectRequest returns 415


I have a server in tomcat and I am trying to make a request from my android device. All I get back is 415. I havent been able to replicate the error with fiddler by sending a defective Json. The server does not react to my request. I do have println() at every step just in case.

                   try {
                        params.put("userId", 2);
                        params.put("latitude", lon1);
                        params.put("longitude", lat1);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    errTextBox.setText(params.toString());

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

                                @Override
                                public void onResponse(JSONObject response) {
                                    msg.setText(response.toString());

                                }
                            }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d("Error: " + error.getMessage());
                            msg.setText("not ok " + error.getMessage());
                        }
                    }) {

                        /**
                         * 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; charset=utf-8");
                            return headers;
                        }

                    };
                    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(60000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                    queue.add(jsonObjReq);

Not a dublicate because my request is done with a JsonObjectRequest. Also removing the charset did not help.

enter image description here


Solution

  • Well, I may have made a boo boo. In my server software I was already parsing the recived string to Json. So the is that I needed to send plain text...

    @Override
                            public Map<String, String> getHeaders() throws AuthFailureError {
                                HashMap<String, String> headers = new HashMap<String, String>();
                                headers.put("Content-Type", "text/plain; charset=utf8");
                                return headers;