Read complete story

How to make Synchronous HTTP Request in Android Using Volley library

Add dependency for Volley library in your build.gradle.

Volley is a network library which makes fast and easier to make HTTP Request for android application. Using Volley library we don’t need to create AsyncTask to make multiple API calls in volley there is RequestQueue where we can enqueue each request. Here in this example, We used Google place autocomplete API. We are going to make synchronous HTTP Request using volley. To make synchronous HTTP request make a RequestFuture object. And make JsonObjectRequest pass this parameter RequestMethod, URL, pass object of type Json and pass RequestFuture instance.

Add your request in RequestQueue. Now, whenever the user makes an HTTP request each request added to RequestQueue.To get response object from the requestFuture we call get() method. And we also set a timeout of let’s say 10 seconds so we don’t block the UI thread indefinitely in case our request times out.

Hope this helps you to understand how we can use volley used to make the synchronous HTTP request.

 

. . .

Comments (2)

Add Your Comment

  • L
    Hi,
    I’m trying your code and I can see the request in my logs however it always throws a java.util.concurrent.TimeoutException and I can’t use the result in object.

    Any idea?

    • manish choudhary
      Please share the source code so I check your implementation and help you.
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close