Using custom Volley POST doesn't return anything
Asked Answered
O

1

1

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.

Overgrow answered 24/4, 2015 at 2:45 Comment(1)
To make things a bit more interesting I've also tried this with GET (modifying my PHP code to expect the same) and am also getting the same results. To test out that its not my PHP I worked up a quick simple html page that passes GET or POST and its returning the JSON Array that I'm looking for.Overgrow
O
0

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.

Overgrow answered 27/4, 2015 at 21:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.