Working POST Multipart Request with Volley and without HttpEntity Working POST Multipart Request with Volley and without HttpEntity android android

Working POST Multipart Request with Volley and without HttpEntity


I rewrite your code @RacZo and @BNK more modular and easy to use like

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("api_token", "gh659gjhvdyudo973823tt9gvjf7i6ric75r76");        params.put("name", "Angga");        params.put("location", "Indonesia");        params.put("about", "UI/UX Designer");        params.put("contact", "angga@email.com");        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);

Check full of code VolleyMultipartRequest at my gist.


Just want to add to the answer. I was trying to figure how to append text fields to the body and created the following function to do it:

private void buildTextPart(DataOutputStream dataOutputStream, String parameterName, String parameterValue) throws IOException {    dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);    dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + parameterName + "\"" + lineEnd);    dataOutputStream.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);    dataOutputStream.writeBytes(lineEnd);    dataOutputStream.writeBytes(parameterValue + lineEnd);}

It is working pretty well.


For those who are struggling to send utf-8 parameters and still no luck, the problem I had was in the dataOutputStream, and change the code of @RacZo to below code:

private void buildTextPart(DataOutputStream dataOutputStream, String parameterName, String parameterValue) throws IOException {        dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);        dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"");        dataOutputStream.write(parameterName.getBytes("UTF-8"));        dataOutputStream.writeBytes(lineEnd);        dataOutputStream.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);        dataOutputStream.writeBytes(lineEnd);        dataOutputStream.write(parameterValue.getBytes("UTF-8"));        dataOutputStream.writeBytes(lineEnd);    }