0

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?

Prasad
  • 69
  • 9
  • look here http://stackoverflow.com/questions/37908320/failed-to-send-parameter-to-php-post-parameter-android/37909623#37909623 – Sohail Zahid Jul 10 '16 at 07:25
  • @SohailZahid In the link, you are not overriding getHeaders() and not sending username and password. I know how to send parameters. But how to send it with credentials is what I am looking for. – Prasad Jul 10 '16 at 07:28
  • why are you overriding `getHeaders` you can send them in params in security encrypted file. – Sohail Zahid Jul 10 '16 at 07:31
  • At the server end, I have configured to intercept the credentials. – Prasad Jul 10 '16 at 07:33
  • no problem you can send the file after applying md5 and decode it again while receiving? weather its not needed on post request. – Sohail Zahid Jul 10 '16 at 07:36
  • Sending every request with md5 and decoding is not right, and not what I am looking for. – Prasad Jul 10 '16 at 07:39
  • apply md5 only on password otherwise best of luck. – Sohail Zahid Jul 10 '16 at 07:40

1 Answers1

0

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.

Endzeit
  • 4,810
  • 5
  • 29
  • 52
  • BackendConnection.uploadDataUrl has the URL of the server – Prasad Jul 10 '16 at 07:38
  • @Prasad I'm aware of that. I just thought that you may also use "localhost" there which wouldn't help much unless the server actually runs on the phone aswell, which is quite unlikely, – Endzeit Jul 10 '16 at 07:41