Search code examples
androiddrupaldrupal-7httprequesthttpresponse

Android, HTTP POST request to Drupal back-end


I have a problem while trying to implement a communication by means of HTTP request. More precisely, I've used the Volley library and this is my client-side request:

StringRequest sr = new StringRequest(Request.Method.POST, "http://mysite/mydrupalservice",
                                        new Response.Listener<String>() {
                                            @Override
                                            public void onResponse(String response) {
                                                Log.w("CheckinFragment. POST call, RESPONSE:", String.valueOf(response));
                                            }
                                        },
                                        new Response.ErrorListener() {
                                            @Override
                                            public void onErrorResponse(VolleyError error) {
                                                VolleyLog.e("CheckinFragment. POST call, ERROR:", String.valueOf(error));
                                            }
                                        }){

                                    @Override
                                    protected Map<String,String> getParams()
                                    {
                                        Map<String,String> params = new HashMap<String, String>();
                                        params.put("action", "something");
                                        params.put("access_token", "something");
                                        params.put("activity", "1");
                                        return params;
                                    }

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

                                // add the request object to the queue to be executed
                                Controller.getInstance().addToRequestQueue(sr);`

This code is correctly executed from Android-side, but the response received is the following:

BasicNetwork.performRequest: Unexpected response code 404 for http://mysite/myservice
android.volley.AuthFailureError.

I've tried to use http://httpbin.org/ to test my client-side code and it works correctly.

So, I thought that the problem might be server-side. If I try to perform a HTTP request with POST parameters, using for example Firefox's extension Poster, I receive the correct response from the server.

Looking at 401 error code, I thought that the service previously created on Drupal would require an authentication by the user, but that option was previously disabled.

I don't know how to solve this problem, anyone does? Thank you for your attention


Solution

  • Maybe try to use url encoded? For example:

        @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"); // try this
            return params;
        }