How to make Synchronous HTTP Request in Android Using Volley library

Save

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.

 

. . .
Discuss on Helpdesk

Leave a Comment

Your email address will not be published. Required fields are marked*


3 comments

  • 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 (Moderator)
      Please share the source code so I check your implementation and help you.
  • sf
    why is that not in documentation
    thx man
  • css.php
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home