Request application/x-www-form-urlencoded using Volley

Asked

Viewed 402 times

0

Good morning guys, I’m trying to make an integration with web_services of a partner, by php everything worked correctly now I want to implement directly on Android using Volley but I’m getting authentication error, follows below my code:

 RequestQueue rq = Volley.newRequestQueue(MainActivity.this);
            String url = url;
            JsonObjectRequest jReq = new JsonObjectRequest(Request.Method.GET, url,null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject2) {

                            try {
                                if (jsonObject2 != null) {
                                    lista.add(jsonObject2.toString());
                                    adapter.notifyDataSetChanged();
                                } else {
                                    Log.i("MaintActivity", jsonObject2.toString());
                                }
                            } catch (Exception e) {
                                Log.i("MaintActivity", e.getMessage().toString());
                            }


                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    if (volleyError != null) {
                        Log.i("MaintActivity", volleyError.getMessage().toString());
                    }

                }
            }) {

                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("ws_key",ws_key);
                    params.put("ws_user",ws_user);
                    params.put("username",username);
                    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;
                }


            };


            jReq.setRetryPolicy(new DefaultRetryPolicy(
                            5000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
                    )
            );
            rq.add(jReq);

It should be noted that debugging, the code is only entering the getHeaders method and does not go through getParams, someone has an idea?

Thanks.

  • Can post the error?

  • Igor, actually the error is returned by the client web_service, returns me a json : {"result":false,"error_message":"Authentication error!" } I don’t have code errors, build errors etc.... is an authentication error.. that is, the data of key, user and username are not arriving in the web service seems. As I said at the end, debug doesn’t go through the getParams function so it doesn’t seem to include the data in the request.

1 answer

1


The JsonObjectRequest does not invoke the method getParams!

I advise you to use the StringRequest in his place staying like this:

StringRequest jReq = new StringRequest(Request.Method.POST, url,
           new Response.Listener<String>() {
           @Override
           public void onResponse(String response) {
                  try {
                      JSONObject jsonObject2 = new JSONObject(response);
                      if (jsonObject2 != null) {

                         lista.add(jsonObject2.toString());
                         adapter.notifyDataSetChanged();
                      } else {
                          Log.i("MaintActivity", jsonObject2.toString());
                      }
                   } catch (Exception e) {
                        Log.i("MaintActivity", e.getMessage().toString());
                   }

                    }
                }, new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError volleyError) {
                            if (volleyError != null) {
                                Log.i("MaintActivity", volleyError.getMessage().toString());
                            }
                     }
            }) {

            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put("ws_key",ws_key);
                params.put("ws_user",ws_user);
                params.put("username",username);
                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;
            }


        };

Another fact that I noticed in your code and that you are indicating in JsonObjectRequest that the passage of parameters will be by GET change to Request.Method.POST now in the StringRequest!

  • 1

    Okay, Igor, I made a String request. It was solved by the client at the end, the authentication part of the other end was tidied up and I was able to pass the data in the ( url to their requests). Thank you.

  • 1

    @Marcelolópez We are here to help each other!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.