简体   繁体   中英

Android Volley How to send username and password with other parameters in a POST request using volley

I have a curl command that works perfectly curl --request POST --user xyz@mail.com:password http://localhost:8080/api/uploadmydata -d 'param1=300'

But I am unable to do the same thing with volley. My current code is:

public static void tryUploading(final Activity activity){

    StringRequest stringRequest = new StringRequest(Request.Method.POST, BackendConnection.uploadDataUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //textPrompt.setText("Wrong User name or Password");
                }
            }){
        @Override
        public Map<String, String> getHeaders() {
            Map<String, String> params = new HashMap<String, String>();
            params.put(
                    "Authorization",
                    String.format("Basic %s", Base64.encodeToString(
                            String.format("%s:%s", "xyz@mail.com", "password").getBytes(), Base64.DEFAULT)));
            params.put("param1", 300);

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(activity);
    requestQueue.add(stringRequest);
}

I am using spring boot as server. In the server above code triggers verification of username and password but fails to reach the intended url later. How to add both user credentails and parameters in volley request like we do in curl --user and -d?

As I can't see the URL used within your app I simply have to quess that you use the same as in your CURL.

Also quessing that your CURL is executed on your computer where your server is running you have to change the IP address in your app accordingly as localhost refers to the same device.

So unless your server doesn't run on your smartphone you have to use the computers IP address, not localhost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM