我创建了一个自定义Volley
Request
类,扩展了Request<NetworkResponse>
。以下是该自定义类的代码:
public class MultipartRequest extends Request<NetworkResponse> {
private final Response.Listener<NetworkResponse> mListener;
private final Response.ErrorListener mErrorListener;
private final Map<String, String> mHeaders;
private final Map<String, String> mParams;
private final String mMimeType;
private final byte[] mMultipartBody;
public MultipartRequest(String url, Map<String, String> headers, Map<String, String> params, String mimeType, byte[] multipartBody, Response.Listener<NetworkResponse> listener, Response.ErrorListener errorListener) {
super(Method.POST, url, errorListener);
this.mListener = listener;
this.mErrorListener = errorListener;
this.mHeaders = headers;
this.mParams = params;
this.mMimeType = mimeType;
this.mMultipartBody = multipartBody;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Log.i("MultipartRequest", "headers = " + mHeaders);
return mHeaders;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Log.i("MultipartRequest", "params = " + mParams);
return mParams;
}
@Override
public String getBodyContentType() {
return mMimeType;
}
@Override
public byte[] getBody() throws AuthFailureError {
return mMultipartBody;
}
@Override
protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) {
try {
return Response.success(
response,
HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(NetworkResponse response) {
mListener.onResponse(response);
}
@Override
public void deliverError(VolleyError error) {
mErrorListener.onErrorResponse(error);
}
}
我在Log.i
和getHeaders()
中插入了getParams()
语句,以确保它们被正确调用。我按如下方式创建请求:
final Map<String, String> volleyParams = new HashMap<>();
volleyParams.put("size", String.valueOf(data.imageSize));
volleyParams.put("width", String.valueOf(data.imageWidth));
volleyParams.put("height", String.valueOf(data.imageHeight));
volleyParams.put("isIndex", updateProfilePhoto ? "1" : "0");
final Context context = this;
final String twoHyphens = "--";
final String lineEnd = "\r\n";
final String boundary = "apiclient-" + System.currentTimeMillis();
final String mimeType = "multipart/form-data;boundary=" + boundary;
byte[] multipartBody = new byte[0];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
try {
// the first file
buildPart(dos, data.thumbnailImageArray, "file0");
// the second file
buildPart(dos, data.scaledImageArray, "file1");
// send multipart form data necesssary after file data
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// pass to multipart body
multipartBody = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
Map<String, String> headers = new HashMap<String, String>();
headers.put("User-Agent", Authentication.getUserAgent());
headers.put("X-XX-API", Authentication.getKey());
MultipartRequest multipartRequest = new MultipartRequest(BASE_URL + "member/photos", headers, volleyParams, mimeType, multipartBody, new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
Log.i(LOG_TAG, "response: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(LOG_TAG, "statusCode = " + error.networkResponse.statusCode);
Log.i(LOG_TAG, "data = " + error.networkResponse.data);
Log.i(LOG_TAG, "headers = " + error.networkResponse.headers);
Log.i(LOG_TAG, "notModified = " + error.networkResponse.notModified);
Log.i(LOG_TAG, "networkTimeMs = " + error.networkResponse.networkTimeMs);
}
});
MySingleton.getInstance(context).addToRequestQueue(multipartRequest);
执行调用,但永远不会调用getParams()
。如何确保始终调用getParams()
?
答案 0 :(得分:0)
我遇到了同样的问题。您需要覆盖getBody方法并将所有参数转换为字节数组。例如,对于JSON请求。
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() {
if (mParams == null) {
return null;
}
try {
return new Gson().toJson(mParams).getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}