Multipart POST Requests with Volley and the Absence of HttpEntity
In Android API22, HttpEntity was deprecated and removed entirely in API23. This article provides a working sample to implement a POST multipart request with Volley without HttpEntity. The proposed solution is tested with Asp.Net Web API.
Implementation Details
The code consists of two classes:
MultipartActivity.java:
MultipartRequest.java:
Additional Features
Adding Text Parts:
As suggested in the provided code, one can add text parts to the multipart request using the getParams() method:
@Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<>(); params.put("param1", "value1"); params.put("param2", "value2"); return params; }
Modular Code for Reusability:
To enhance reusability, the code has been refactored:
class VolleyMultipartRequest extends Request<NetworkResponse> { // ... other methods @Override protected Map<String, DataPart> getByteData() { // ... code for adding byte parts } }
Usage Example:
VolleyMultipartRequest request = new VolleyMultipartRequest(Method.POST, url, new Response.Listener<NetworkResponse>() { // ... listener code }, new Response.ErrorListener() { // ... error listener code }) { @Override protected Map<String, String> getParams() { // ... } @Override protected Map<String, DataPart> getByteData() { // ... } };
The above is the detailed content of How to Make Multipart POST Requests with Volley in Android After HttpEntity's Removal?. For more information, please follow other related articles on the PHP Chinese website!