Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

POST JsonObject with different data types (int, boolean etc.) params and get JsonArray response #169

Open
emreturgut opened this issue Jan 3, 2017 · 2 comments

Comments

@emreturgut
Copy link

emreturgut commented Jan 3, 2017

I had to call json array post request with integer and string params. Firstly I tried with string request but when string request is used your hash map params must be string values.

After that I solved the problem with this way;

I have create CustomRequest class;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class CustomRequest extends JsonRequest<JSONArray> {

    public CustomRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener) {
        super(Method.GET, url, null, listener, errorListener);
    }

    public CustomRequest(int method, String url, JSONObject jsonRequest,
                            Listener<JSONArray> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                errorListener);
    }

    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
            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));
        }
    }
}

and I used the class like this way;

CustomRequest customRequest = new CustomRequest(Request.Method.POST, url,  (params != null) ? new JSONObject(params) : null, new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                callback.onSuccessResponse( response);

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                
                callback.onErrorResponse(error);

            }
        }){

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {

                HashMap<String, String> params = new HashMap<String, String>();
                

                return params;

            }
        };

I hope this solution also helps someone else who has this problem

@Dave-Algorism
Copy link

Not working for me. My issue is, where did u get params in (params != null) ? new JSONObject(params) : null from? I have followed your code to solve this exact problem. Also, what about

@OverRide
protected Map<String, String> getParams() throws AuthFailureError {
// Posting parameters to login url
Map<String, String> params = new HashMap<>();
params.put("email", mEmail);
params.put("password", mPassword);

              return params;
        }

for example?

@israilbony
Copy link

Thank you great code boss

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants