I have a custom volley request, but it didn't take my params when sending request, what's wrong with my code?
I set a breakpoint at getParams
and getPostParams
, but none of them went through.
I used com.mcxiaoke.volley:library:1.0.+
which is forked from google volley to support maven.
I find the class worked well at real device but cann't work at genymotion.
public class GsonRequest<T> extends Request<T> {
private Class<T> clazz;
private Map<String, String> headers;
private Map<String, String> params;
private Listener<T> listener;
public GsonRequest(Api api, Class<T> clazz, Listener<T> listener, ErrorListener errorListener) {
this(api, clazz, null, null, listener, errorListener);
}
public GsonRequest(Api api, Class<T> clazz, Map<String, String> params, Listener<T> listener,
ErrorListener errorListener) {
this(api, clazz, params, null, listener, errorListener);
}
/**
* Make a GET request and return a parsed object from JSON.
*
* @param url
* URL of the request to make
* @param clazz
* Relevant class object, for Gson's reflection
* @param headers
* Map of request headers
*/
public GsonRequest(Api api, Class<T> clazz, Map<String, String> params, Map<String, String> headers,
Listener<T> listener, ErrorListener errorListener) {
super(api.method, api.url, errorListener);
this.clazz = clazz;
this.params = params;
this.headers = headers;
this.listener = listener;
}
// use new GsonRequest()
@Deprecated
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers, Listener<T> listener,
ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String, String> result = params;
return result;
}
@Override
public Map<String, String> getPostParams() throws AuthFailureError {
Map<String, String> result = params;
return result;
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Log.d("zhch", json);
return Response.success(GsonUtils.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
See Question&Answers more detail:
os