Om Infowave Developers
Om Infowave Developers

Reputation: 1565

How to pass String and File data with Volley POST method in Android?

I am using Volley to make server requests but i came to know that volley is not passing params from getParams() method in POST request, so now i am passing these data by concatenating all param/values with the url like bellow.

String url = "http://myweb/api/work?"+param1+"="+value;

Now the problem is it works fine with text data only, i can make request successfully and all params are passing to server but now i have to upload some image file also using same api.

How can i pass a file and string data using Volley POST method?

Following are the solutions i tried but got no success. https://gist.github.com/anggadarkprince/a7c536da091f4b26bb4abf2f92926594#file-volleymultipartrequest-java

https://www.simplifiedcoding.net/android-volley-tutorial-to-upload-image-to-server/

Edit Following is my current request code:

StringRequest request = new StringRequest(Request.Method.POST, uri + param, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            dismissProgressDialog();
            printResponse(response);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            dismissProgressDialog();
            error.printStackTrace();

        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("key", key);
            headers.put("tkey", tkey);
            headers.put("Content-Type", "application/multipart");
            return headers;
        }
    };

Upvotes: 2

Views: 5458

Answers (2)

Fakhriddin Abdullaev
Fakhriddin Abdullaev

Reputation: 4910

VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(com.android.volley.Request.Method.POST, url, new Response.Listener<NetworkResponse>() {

        @Override
        public void onResponse(NetworkResponse response) {
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    })
    {
        //pass String parameters here
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("category", "image");
            params.put("p_name", "myImage");
            return params;
        }

        //pass header
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();

            headers.put("key", key);
            headers.put("tkey", tkey);
            headers.put("Content-Type", "application/multipart");

            return headers;
        }

        //pass file here (*/* - means you can pass any kind of file)
        @Override
        protected Map<String, VolleyMultipartRequest.DataPart> getByteData() {
            Map<String, DataPart> up_params = new HashMap<>();
            up_params.put("params", new DataPart(file_path, file_name, "*/*"));

            return up_params;
        }
    };      
 VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(multipartRequest);

Upvotes: 0

sam chaudhari
sam chaudhari

Reputation: 776

Use Following code

VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
    @Override
    public void onResponse(NetworkResponse response) {
        String resultResponse = new String(response.data);
        // parse success output
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {                
        error.printStackTrace();
    }
}) {
    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("name", "Sam");
        params.put("location", "India");
        params.put("about", "UI/UX Designer");
        params.put("contact", "[email protected]");
        return params;
    }

    @Override
    protected Map<String, DataPart> getByteData() {
        Map<String, DataPart> params = new HashMap<>();
        // file name could found file base or direct access from real path
        // for now just get bitmap data from ImageView
        params.put("avatar", new DataPart("file_avatar.jpg", AppHelper.getFileDataFromDrawable(getBaseContext(), mAvatarImage.getDrawable()), "image/jpeg"));
        params.put("cover", new DataPart("file_cover.jpg", AppHelper.getFileDataFromDrawable(getBaseContext(), mCoverImage.getDrawable()), "image/jpeg"));

        return params;
    }
};

VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(multipartRequest);

Upvotes: 0

Related Questions