2

enter image description here

like this, I want to send JSON body request to GET API.

Tried this but it did not work:

 public static void getQuestionsListApi2(final String requestId, final String timestamp,
                                        final ImageProcessingCallback.downloadQuestionsCallbacks callback,
                                        final Context context) {

    try {
       String url = NetUrls.downloadQuestions;

        final JSONObject jsonBody = new JSONObject();
        jsonBody.put("requestId", requestId);
        jsonBody.put("timestamp", timestamp);
        final String mRequestBody = jsonBody.toString();
        Log.i("params", String.valueOf(jsonBody));
        Log.i("URL", url);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, **jsonBody**, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.v("TAG", "Success " + jsonObject);
                callback.downloadQuestionsCallbacksSuccess(jsonObject.toString());
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.v("TAG", "ERROR " + volleyError.toString());
            }


        });

        request.setRetryPolicy(new DefaultRetryPolicy(
                DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 0,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        RequestQueue queue = Volley.newRequestQueue(context);
        queue.add(request);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}


        request.setRetryPolicy(new DefaultRetryPolicy(
                DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 0,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


        RequestQueue queue = Volley.newRequestQueue(context);
        queue.add(request);
   

Here is the code that I am using when sending JSONRequest. with GET method I am getting 400 error response from server and server does not accept the the data in the URL form. I am sending The jsonBody object as parameter. Any solution?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kumar Jadhav
  • 109
  • 2
  • 6

2 Answers2

0

If you want to pass Json data in body of GET request, you have to use Query annotation

Call<YourNodelClass> getSomeDetails(@Query("threaded") String threaded, @Query("limit") int limit);

this will pass as Json object {"threaded": "val", "limit": 3}.

i have tried and this one is only working code.

Avinash Ajay Pandey
  • 1,497
  • 12
  • 19
-1

You can use retrofit to send request with body. http://square.github.io/retrofit/

It is easy to use library, example:

@GET("[url node]")
Single<Response<ResponseBody>> doSmt(@Header("Authorization") String token, @Body ListRequest name);

Also, take a look here about get methods with body HTTP GET with request body

UPDATE

GET method with request body is optional here. However, this RFC7231 document says,

sending a payload body on a GET request might cause some existing implementations to reject the request.

which means this isn't recommended. Use POST method to use request body.

Check this table from wikipedia.

Table from wikipedia with http methods

Community
  • 1
  • 1
Maksym V.
  • 2,877
  • 17
  • 27