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

0

Hello, I'm trying to do a web_services integration with a partner, for php everything worked correctly now I want to implement directly on Android using volley however I'm getting authentication error, here is my code below:

 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's worth noting that debugging, the code is only getting into the getHeaders method and does not get through the getParams, does anyone have an idea?

Thank you.

    
asked by anonymous 10.08.2016 / 16:58

1 answer

1

JsonObjectRequest does not invoke method getParams !

I advise you to use StringRequest instead of it, thus:

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% with% that the parameter passing will be by JsonObjectRequest change to GET now in Request.Method.POST !

    
11.08.2016 / 00:25