问题
I'm trying to use a custom Class that extend the JsonRequest class to send a JSONArrayRequest using POST and a parameter.
public class MethodJsonArrayRequest extends JsonRequest<JSONArray> {
public MethodJsonArrayRequest(int method, String url, JSONObject params, com.android.volley.Response.Listener<org.json.JSONArray> listener, ErrorListener errorListener) {
super(method, url, params.toString(), listener, errorListener);
Log.d("method", Integer.toString(method));
Log.d("jsonRequest", params.toString());
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
My logs return this:
D/method﹕ 1
D/jsonRequest﹕ {"idShow":"219"}
I'm passing this info to my custom class with this snippit:
...
JSONObject params = new JSONObject();
try {
params.put("idShow", idShow);
}
catch (JSONException e) {Log.d("JSON e", e.getMessage()); }
MethodJsonArrayRequest episodeRequest = new MethodJsonArrayRequest(Request.Method.POST, episodeURL, params, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray myResponse) {
try {
Log.d("myResponse", myResponse.toString());
...
Log of myResponse:
D/myResponse﹕ []
But for whatever reason it does not return anything, I feel like I might not be passing the right thing in for the paramas but I'm not sure, any help is greatly appreciated! Let me know if there is something I didn't include here that might be helpful.
回答1:
user98239820 answer here was extreamly useful. Instead of extending JsonRequest<JSONArray>
class he extended the Request
class. I also had to change his new JSONObject
to new JSONArray
to fit my needs, but by pointing to that class works perfectly.
来源:https://stackoverflow.com/questions/29837859/using-custom-volley-post-doesnt-return-anything