0

I'm trying to found a solution to make a GET call with a Json body to filter data. I think that's an error but i have a GET request where i need to pass a Json body like {filter: 1}. At the moment I'm using Volley but i don't have found any way to send the body during a GET, Retrofit was crashing during this kind of operation and with the apache libraries org.apache.http.client.methods.HttpEntityEnclosingRequestBase; and org.apache.http.client.methods.HttpGet; Android show me all the needed methods as deprecated.

This is my request method with Volley:

public void getVenuesListOfTreatments(String venueId, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener){
        String url = baseUrl + "shop/"+Id+"/example.json";

        Map<String, String> param = new HashMap<>();
            param.put("with_staff_members", "1");
            ArrayRequest request = new ArrayRequest(Request.Method.GET, url, param, listener, errorListener){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<>();
                    headers.put("Content-Type","application/json");
                    return headers;
                }
            };

            request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
            MyApp.getInstance().addToRequestQueue(request);
    }


public class ArrayRequest extends Request<JSONArray>  {

    private Response.Listener<JSONArray> listener;
    private Map<String, String> params;

    public ArrayRequest(String url, Map<String, String> params,
                       Response.Listener<JSONArray> reponseListener, Response.ErrorListener errorListener) {
        super(Request.Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

        public ArrayRequest(int method, String url, Map<String, String> params,
                           Response.Listener<JSONArray> reponseListener, Response.ErrorListener errorListener) {
            super(method, url, errorListener);
            this.listener = reponseListener;
            this.params = params;
        }

        protected Map<String, String> getParams()
                throws com.android.volley.AuthFailureError {
            return params;
        };

        @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));
            }
        }

        @Override
        protected void deliverResponse(JSONArray response) {
            // TODO Auto-generated method stub
            listener.onResponse(response);
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type","application/json");
            return headers;
        }
    }

There is any solution for that? Or is not possible to pass a body during a GET ?

UPDATE: I'm trying to make a curl call opening the android shell but it return me always an empty String ""

public String executeCommand() {

        String command = "curl --request GET --url http://example.com/api/v1/shop/5/example.json --header 'accept: application/json' --header 'content-type: application/json' --data '{\"with_staff_members\": true}'";


        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        String outputString = output.toString();

        return outputString;

    } 
davidev
  • 304
  • 8
  • 20
  • 1
    `GET call with a Json body`, doesn't sound really good. How about you use query to filter your data ? – Blackbelt Oct 20 '15 at 09:12
  • not a good idea to have [body in get](http://stackoverflow.com/a/983458/1529129) – Rahul Tiwari Oct 20 '15 at 09:14
  • i know is not a good idea but the API services is not developed by me i know this is wrong we have the POST for that but they don't want to change that call from GET to POST so i need to found a solution (if there is any one!) – davidev Oct 20 '15 at 09:18
  • Do you mean that the server side app (web service) supports GET request with body? If so, try overriding "getBody" of your volley request and check if it works or not. – BNK Oct 20 '15 at 10:28
  • Yes the server supports GET request with body and i've already try to override the getBody but doesn't work – davidev Oct 20 '15 at 10:45
  • Check this... http://stackoverflow.com/questions/978061/http-get-with-request-body?lq=1 better to change that get request to post for avoiding problems – Jackson Chengalai Oct 20 '15 at 11:31
  • I've already see this solution, but i don't have found nothing there and Unfortunately the people who develop the server don't want to change that he know is an error but the iOS APP and the Front-end are working with that call from the server. – davidev Oct 20 '15 at 11:35
  • Have you tried some tools such as Postman in Chrome to check if the server app really supports such GET requests? – BNK Oct 20 '15 at 14:22
  • Yes it supports GET request with iOS and React (on front-end) with a body in a GET call like this one: {example: true} , also with curl it's working and i'm trying to do the command from the android command line but it's not working.. – davidev Oct 20 '15 at 14:41

0 Answers0