我有一个关于堆栈溢出的线程here,我已经通过Volley使用多部分帖子解决了我的问题。问题是,我所做的是String
请求,我希望将其更改为JSONObject
请求,因为我需要捕获服务器的响应。
UPADATE:我还尝试将所有Response<String>
更改为Response<JSONObject>
这是我在parseNetworkResponse(NetworkResponse response)
方法的新实现:
@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
public void onErrorResponse(VolleyError error) {
Log.i("Error",error.toString());
}
显示的错误是:
com.android.volley.ParseError: org.json.JSONException: Value <div of type java.lang.String cannot be converted to JSONObject
答案 0 :(得分:0)
正如我在上一个问题中的回答,我建议您创建一个自定义请求,以响应NetworkResponse
或JSONObject
,如下所示:
MultipartRequest.java:
class MultipartRequest extends Request<NetworkResponse> {
private final Response.Listener<NetworkResponse> mListener;
private final Response.ErrorListener mErrorListener;
private final Map<String, String> mHeaders;
private final String mMimeType;
private final byte[] mMultipartBody;
public MultipartRequest(String url, Map<String, String> headers, 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.mMimeType = mimeType;
this.mMultipartBody = multipartBody;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return (mHeaders != null) ? mHeaders : super.getHeaders();
}
@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);
}
}
在这里,您可以创建自定义MultipartRequest extends Request<JSONObject>
希望这有帮助!
更新您的评论:
我有一个多部分实体已经包含在apache http组件库中。有没有其他选择?
以下是我的HttpEntity
请求,并返回JSONArray
。我认为如果您愿意,可以自定义返回JSONObject
。
private void makeJsonArrayRequest(Context context, int method, String url, HttpEntity httpEntity, final VolleyResponseListener listener) {
JSONObject jsonRequest = null;
String stringEntity;
try {
stringEntity = EntityUtils.toString(httpEntity);
if (stringEntity != null) {
jsonRequest = new JSONObject(stringEntity);
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(method, url, jsonRequest, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
listener.onResponse(jsonArray);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError(getErrorMessage(error));
}
}) {
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
...
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
...
}
};
// Access the RequestQueue through singleton class.
MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
}