0

I am new in android studio and I am going to use volley library to get data from server.

json request is follow:

{
  "method": "authenticate",
  "params": [
    "dummyuser",
    "dummy"
  ],
  "id": "1",
  "jsonrpc": "2.0"
}

Following is my code

RequestQueue requestQueueV1= Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequestV1 = new StringRequest(Request.Method.POST, server_url + AppConfig.VERSION1_URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
            // response is JSONObject
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(SettingsActivity.this, "Error occurred.", Toast.LENGTH_LONG).show();
    }
}) {
     @Override
     protected Map<String, String> getParams() {
         Map<String, String> request_body = new HashMap<String, String>();
         request_body.put("method", "authenticate");
         request_body.put("id", "1");
         request_body.put("jsonrpc", "2.0");
         JSONArray params = new JSONArray();
         params.put(user_name);
         params.put(password);
         request_body.put("params", params.toString());
         return request_body;
    }
    @Override
    public String getBodyContentType() {
         return "application/json; charset=utf-8";
    }

};
requestQueueV1.add(stringRequestV1);

I have error "com.android.volley.TimeoutError". How can I fix this error?

And follow is response

{
    "result": {
        "success": true,
        "items": [
            {
                "apiKey": "4902ad6c957144aba697995f8",
                "userID": "41426322"
            }
        ],
        "total": 1
    },
    "id": "1",
    "jsonrpc": "2.0"
}

How can I pass this response json object when fetching is success.

Following is my postman screenshot:

postman screenshot1

postman screenshot2

Water Flower
  • 377
  • 7
  • 23

1 Answers1

0

Add this below your stringRequestV1 before adding it to Requestqueue.

stringRequestV1.setRetryPolicy(new DefaultRetryPolicy(4000, 2, 2f));

Where 4000 is TIMEOUT_MS, 2 is DEFAULT_MAX_RETRIES, 2f is DEFAULT_BACKOFF_MULT.

You can use default volley timeout settings like below.

stringRequestV1.setRetryPolicy(new DefaultRetryPolicy(
5000, 
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Roon13
  • 387
  • 2
  • 23
  • Thanks for ur reply. I have add ur comment. before adding RequestQueue. But I have get error: com.android.volley.AuthFailureError. – Water Flower May 29 '19 at 14:56
  • Its because of your API needs some data in headers while requesting data. Follow [https://stackoverflow.com/questions/31891604/com-android-volly-authfailureerror-in-making-basic-volly-post-request-to-a-djang] – Roon13 May 29 '19 at 15:01
  • There is no header. I added the screenshot of my postman. Please check and advance me. – Water Flower May 29 '19 at 15:03
  • Try this. I am not sure about this but Instead of getParams() try getHeaders(). – Roon13 May 29 '19 at 15:16
  • As u can see in screenshot, that is request body, not header – Water Flower May 29 '19 at 15:22
  • I see that @WaterFlower. Hold on – Roon13 May 29 '19 at 15:24
  • You are getting this error because you are not satisfying API requirements. Show me your logcat. Try printing your `request_body`. – Roon13 May 29 '19 at 15:31
  • Try sending this JsonObject. JSONObject obj = new JSONObject(request_body); return obj; Instead of returning request_body – Roon13 May 29 '19 at 15:41
  • @WaterFlower accept it as a answer as I resolved your original question. Also I am sure that you are satisfying API request. You are not sending Json object in the request. – Roon13 May 29 '19 at 16:12