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