0
我正面臨使用多部分表單數據上傳文件的問題。我用api和我現在的代碼給出了ARC的屏幕截圖。任何人的幫助將不勝感激。無法使用多部分/表單數據上傳文件
public class MultiPortRequest<T> extends Request<T> {
private static final String FILE_PART_NAME = "file";
private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create();
private final Response.Listener<String> mListener;
private final File mImageFile;
protected Map<String, String> headers;
public MultiPortRequest(String url,Response.Listener<String> listener,
ErrorListener errorListener,
File imageFile)
{
super(Method.POST, url, errorListener);
mListener = listener;
mImageFile = imageFile;
buildMultipartEntity();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = super.getHeaders();
if (headers == null
|| headers.equals(Collections.emptyMap())) {
headers = new HashMap<String, String>();
}
headers.put("Identity", "rHTYsg8vZ/KWTaGOsy0eMNhngJMiZiK60pd9jAUQ+fI=");
return headers;
}
private void buildMultipartEntity()
{
mBuilder.addBinaryBody(FILE_PART_NAME, mImageFile, ContentType.create("multipart/form-data"),
mImageFile.getName());
mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));
}
@Override
public String getBodyContentType()
{
String contentTypeHeader = mBuilder.build().getContentType().getValue();
return contentTypeHeader;
}
@Override
public byte[] getBody() throws AuthFailureError
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
mBuilder.build().writeTo(bos);
}
catch (IOException e)
{
VolleyLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request.");
}
return bos.toByteArray();
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response)
{
try {
String result = null;
result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return (Response<T>) Response.success(new JSONObject(result), 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(T response)
{
mListener.onResponse(response.toString());
Log.v("yes", String.valueOf(mListener));
}
}
日Thnx很多關於我們的代碼!它的工作:) –