我可能是错了这一点,但我认为你需要实现自己的com.android.volley.toolbox.HttpStack为此,因为默认的(HurlStack如果版本> Gingerbread或HttpClientStack)不处理multipart / form-data 。
编辑:
确实我错了。我能够使用MultipartEntity在请求这样做:
public class MultipartRequest extends Request {
private MultipartEntity entity = new MultipartEntity();
private static final String FILE_PART_NAME = "file";
private static final String STRING_PART_NAME = "text";
private final Response.Listener mListener;
private final File mFilePart;
private final String mStringPart;
public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener listener, File file, String stringPart)
{
super(Method.POST, url, errorListener);
mListener = listener;
mFilePart = file;
mStringPart = stringPart;
buildMultipartEntity();
}
private void buildMultipartEntity()
{
entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
try
{
entity.addPart(STRING_PART_NAME, new StringBody(mStringPart));
}
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 parseNetworkResponse(NetworkResponse response)
{
return Response.success("Uploaded", getCacheEntry());
}
@Override
protected void deliverResponse(String response)
{
mListener.onResponse(response);
}
}
它是相当原始,但我试图与一个图像和一个简单的字符串,它的工作原理。响应是一个占位符,在这种情况下返回响应字符串没有什么意义。我有问题使用apache httpmime使用MultipartEntity所以我用这个https://code.google.com/p/httpclientandroidlib/不知道如果有一个更好的方法。
希望它有帮助。
编辑
你可以使用httpmime而不使用httpclientandroidlib,唯一的依赖是httpcore。