Android Volley POST string in body

前端 未结 3 1379
走了就别回头了
走了就别回头了 2020-12-03 01:42

I\'m trying to use Volley library to communicate with my RESTful API.

I have to POST string in the body, when I\'m asking for the bearer Token. String should look li

相关标签:
3条回答
  • 2020-12-03 02:27

    I know this is old, but I ran into this same problem and there is a much cleaner solution imo found here: How to send a POST request using volley with string body?

    0 讨论(0)
  • 2020-12-03 02:30

    To send a normal POST request (no JSON) with parameters like username and password, you'd usually override getParams() and pass a Map of parameters:

    public void HttpPOSTRequestWithParameters() {
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "http://www.somewebsite.com/login.asp";
        StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
            new Response.Listener<String>() 
            {
                @Override
                public void onResponse(String response) {
                    Log.d("Response", response);
                }
            }, 
            new Response.ErrorListener() 
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("ERROR","error => "+error.toString());
                }
            }
                ) {     
            // this is the relevant method
            @Override
            protected Map<String, String> getParams() 
            {  
                Map<String, String>  params = new HashMap<String, String>();
                params.put("grant_type", "password"); 
                // volley will escape this for you 
                params.put("randomFieldFilledWithAwkwardCharacters", "{{%stuffToBe Escaped/");
                params.put("username", "Alice");  
                params.put("password", "password123");
    
                return params;
            }
        };
        queue.add(postRequest);
    }
    

    And to send an arbitary string as POST body data in a Volley StringRequest, you override getBody()

    public void HttpPOSTRequestWithArbitaryStringBody() {
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "http://www.somewebsite.com/login.asp";
        StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
            new Response.Listener<String>() 
            {
                @Override
                public void onResponse(String response) {
                    Log.d("Response", response);
                }
            }, 
            new Response.ErrorListener() 
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("ERROR","error => "+error.toString());
                }
            }
                ) {  
             // this is the relevant method   
            @Override
            public byte[] getBody() throws AuthFailureError {
                String httpPostBody="grant_type=password&username=Alice&password=password123";
                // usually you'd have a field with some values you'd want to escape, you need to do it yourself if overriding getBody. here's how you do it 
                try {
                    httpPostBody=httpPostBody+"&randomFieldFilledWithAwkwardCharacters="+URLEncoder.encode("{{%stuffToBe Escaped/","UTF-8");
                } catch (UnsupportedEncodingException exception) {
                    Log.e("ERROR", "exception", exception);
                    // return null and don't pass any POST string if you encounter encoding error
                    return null;
                }
                return httpPostBody.getBytes();
            }
        };
        queue.add(postRequest);
    }
    

    As an aside, Volley documentation is non-existent and quality of StackOverflow answers is pretty bad. Can't believe an answer with an example like this wasn't here already.

    0 讨论(0)
  • 2020-12-03 02:34

    First thing, I advise you to see exactly what you're sending by either printing to the log or using a network sniffer like wireshark or fiddler.

    How about trying to put the params in the body? If you still want a StringRequest you'll need to extend it and override the getBody() method (similarly to JsonObjectRequest)

    0 讨论(0)
提交回复
热议问题