I already have a sub class of Request<JSONObject>
that is used for http post to the server. The problem is, I have no idea on how can I add a parameter for a file. Posting string to the server is easy. but I need to add file as a different parameter. How can I do it?
public class AddNewPetRequest extends Request<JSONObject> {
private Response.Listener<JSONObject> listener;
public AddNewPetRequest(String url, Map<String, String> params,
Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(Request.Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public AddNewPetRequest(int method, String url, Map<String, String> params,
Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
UPDATE QUESTION:
I followed the pattern of one of the answers here in stackoverflow and I came up with this implementation:
public class MultipartRequest extends Request<String> {
private MultipartEntity entity = new MultipartEntity();
private final Response.Listener<String> mListener;
private HashMap<String, String> mParams;
public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener)
{
super(Method.POST, url, errorListener);
mListener = listener;
buildMultipartEntity();
}
private void buildMultipartEntity()
{
entity.addPart("profile_picture", new FileBody(new File("/storage/emulated/0/Pictures/VSCOCam/2015-07-31 11.55.14 1.jpg")));
try
{
entity.addPart("user_id", new StringBody("15"));
entity.addPart("name", new StringBody("Bogs"));
entity.addPart("gender", new StringBody("Male"));
entity.addPart("date_birth", new StringBody("1999-12-5"));
entity.addPart("breed", new StringBody("monkey"));
}
catch (UnsupportedEncodingException e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}
@Override
public String getBodyContentType()
{
return entity.getContentType().getValue();
}
@Override
public byte[] getBody() throws AuthFailureError
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
entity.writeTo(bos);
}
catch (IOException e)
{
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
{
return Response.success("Uploaded", getCacheEntry());
}
@Override
protected void deliverResponse(String response)
{
mListener.onResponse(response);
}
When I add this request to my request queue, it responded with a com.android.volley.TimeoutError
but if a check the data base, the request executes and add items to the table but the profile picture upload has only 1 byte of size. another problem, my database item added twice.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…