0

Want to send a single parameter and receive a string type result but it doesn't work.

I tested my RESTfull API with this Url in a browser and it works fine:

http://192.168.1.20:88/Home/testme?a=abc

But when want to use it in Android Studio(Java language) it does not work and the Exception message is null. After reading some posts like this, I''m confused should I use JSON type in sending? If yes, why?

String url = "http://192.168.1.20:88/Home/testme";
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        tVMessage.setText(response);
                    }
                }, new Response.ErrorListener() {
                   @Override
                    public void onErrorResponse(VolleyError error) {
                              tVMessage.setText("That didn't work!"+error.getMessage());
                    }
        }){
            @Override
            protected  Map<String,String> getParams() {
                Map<String,String> params = new HashMap<String, String>();
                params.put("a","test value");
                return params;
            }

           @Override
            public  Map<String,String> getHeaders() {
               Map<String,String> params = new HashMap<String, String>();
               params.put("Content-Type","application/x-www-form-urlencoded");
                return params;
            }
        };
        queue.add(stringRequest);
motevalizadeh
  • 5,244
  • 14
  • 61
  • 108

2 Answers2

1

I think you are using a GET request not POST

So just change the URL to this

String url = "http://192.168.1.20:88/Home/testme?a=abc";

And change Request.Method.POST to Request.Method.GET

AgentP
  • 6,261
  • 2
  • 31
  • 52
1

You need change your url to be :

String url = "http://192.168.1.20:88/Home/testme?a=abc";

Because its a GET request, getParams() function only used in POST request

Zukron Alviandy
  • 106
  • 2
  • 3