Davood
Davood

Reputation: 5645

In Android ,Volley , How to pass a single string Parameter and receive a string result?

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);

Upvotes: 0

Views: 343

Answers (2)

Zukron Alviandy
Zukron Alviandy

Reputation: 106

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

Upvotes: 1

AgentP
AgentP

Reputation: 7240

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

Upvotes: 1

Related Questions