1

I'm trying to connect to an API using volley, I'm setting all the parameters and headers, but it seems that the params are being ignored, what am I missing here? I started learning android volley last week and I'm kind of lost.

    package com.rep.app.principal;

    import android.os.AsyncTask;
    import android.os.Bundle;

    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;

    import com.actionbarsherlock.app.SherlockFragmentActivity;
    import com.android.volley.AuthFailureError;
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.VolleyLog;
    import com.android.volley.toolbox.JsonObjectRequest;
    import com.android.volley.toolbox.Volley;
    import com.rep.R;


    import org.json.JSONObject;

    import java.util.HashMap;

    import java.util.Map;


    public class InicioActivity extends SherlockFragmentActivity {


       RequestQueue queue = null;




        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            txtDisplay = (TextView) findViewById(R.id.txtDisplay);

            queue=Volley.newRequestQueue(this);


            AutenticacaoLocalTask mAutenticacaoLocalTask = new AutenticacaoLocalTask();
            mAutenticacaoLocalTask.execute((Void) null);

        }
        private TextView txtDisplay;



        public class AutenticacaoLocalTask extends AsyncTask<Void, Void, Boolean> {

            @Override
            protected Boolean doInBackground(Void... params) {


                try {


                    txtDisplay = (TextView) findViewById(R.id.txtDisplay);

                    String url = "http://192.168.1.18/opa/api/";


                   JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                System.out.println(response);

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

                            }
                        })

                   {

                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {

                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkDaaDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");

                        return headers;
                    }

   @Override 
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email", "rm@test.com.br");
                    params.put("senha", "aaa");

                    return params;
                }
          };    

              queue.add(jsObjRequest);
                    return true;

                } catch (Exception e) {
                    Log.e("RM", e.getMessage());
                    return false;
                }

            }

            @Override
            protected void onPostExecute(final Boolean success) {

            }

            @Override
            protected void onCancelled() {

            }
        }


    }
AND4011002849
  • 1,971
  • 8
  • 38
  • 81
  • It seem you are asking two duplicate questions, [here](http://stackoverflow.com/questions/24285544/android-volley-http-request-custom-header) is your previous question and you accepted an answer, why you still ask this? – VinceStyling Jun 26 '14 at 03:46

2 Answers2

3

It is obvious that sometimes we need to submit request parameters while hitting the url. To do that we have to override getParams() method which should return list of parameters to be send in a key value format.

So,Override getParams() in JsonObjectRequest as

            @Override 
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("email", "rm@test.com.br");
                params.put("senha", "aaa");

                return params;
            }

i.e. use below code as

 JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                System.out.println(response);

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

                            }
                        })

                   {

                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {

                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkDaaDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");

                        return headers;
                    }

   @Override 
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email", "rm@test.com.br");
                    params.put("senha", "aaa");

                    return params;
                }
          };

For more info see Android working with Volley Library

EDIT:

401 is the status code for "unauthorized". If you are getting a 401 while trying an HTACCESS see this question. You need to pass the parameters using an Authenticator.

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

Instead of putting params along a request, try with the third parameter of the JsonObjectRequest object, which is a JSON object (jsonobj in my code).

Something like:

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonobj, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        System.out.println("onResponse()");

        ...
    }
});

Check my answer here.

Luka Govedič
  • 399
  • 4
  • 14