Return response data from function “onResponse” in Volley Library

Save

Volley East, Fast Networking for Android
Android volley is a networking library, which was introduced to make networking calls easier, faster without writing a lot of codes.

There are many features in Volley:

It integrates easily with any protocol and comes out of the box with support for raw strings, images, and JSON.

So here, We will know how to return data from onResponse() of Volley to our function:

1. Make an interface: 

first of all, make an interface named VolleyCallback and interface method named onSuccessResponse.

2. How to use interface in our volley onResponse() 

Here, I have made a function named getResponse(…, …, …, VolleyCallback callback) with VolleyCallback interface as a parameter, then

In above code, getResponse() have 4 parameters:

1- int method – which define your method type which can be GET, POST, PUT, DELETE.

2- String url – this is your url, where you want to send a request and get a response.

3- JSONObject jsonValue this is your JSON data which will be used when you send the post request. 

4- VolleyCallback callback – This is your interface object.

and in onResponse(String Response) you can pass the Response value to the interface function onSuccessResponse(Response). Like

If your application makes constant use of the network, it’s probably most efficient to set up a single instance of RequestQueue that will last the lifetime of your app. so the code

MySingleton.java

 

3. How can you get the return data response in your function:

Here, I have made a function named myWebServiceFun() and call it whenever you want.

In above code,

we have to override the function onSuccessResponse(). And you get a result as a response. Here, I have received a JSON data. And show the message which has sent from API function.

 

 

 

 

 

 

 

. . .
Discuss on Helpdesk

Leave a Comment

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


16 comments

  • Emmanuel Abia
    Wow…i was doing the same thing but got lost. This helped me….Thanks
    • Aman Gupta
      We are very glad to know our blog is needful for you. You are welcome @emmanuelabia:disqus.
  • João Vitor de Camargo
    Just a question: in your function myWebServiceFun(), what is “GetVolleyResponse”? Is this a class type from Volley library? I can’t find it and I’m using a really updated library version (1.0.19).
    • Aman Gupta
      No, GetVolleyResponse is not the class of Volley Library. This is a my class where I create a function named getResponse.
      And VolleyCallback is an interface. And I have used
      new GetVolleyResponse.VolleyCallback(){

      }
      in myWebServiceFun() for override the interface function onSuccessResponse(…) for getting the result.

      • João Vitor de Camargo
        Hi, Amam. Actually, after that, I found a way to do what I had to do. Anyway, thanks a lot for your post and answer!!
        • Aman Gupta
          Ok João Vitor de Camargo. You are most welcome.
      • Esdras de Morais da Silva
        Please, Could you give us a example to GetVolleyResponse implementation with VolleyCallback override implementation?
  • Milad
    hi bro
    just can i say thank youuu
  • Esdras de Morais da Silva
    Please, Could you give us a example to GetVolleyResponse implementation with VolleyCallback override implementation?
    • Aman Gupta (Moderator)
      GetVolleyResponse is not the class of Volley Library. This is my class where I create a function named getResponse. And VolleyCallback is an interface. And I have used new GetVolleyResponse.VolleyCallback(){ … }

      onSuccessResponse(…) for getting the result.

  • Esdras de Morais da Silva
    all interface need to be implementated, how can i do with VolleyCallback interface?
  • Esdras de Morais da Silva
    I cannot resolve error compilation to GetVolleyResponse.VolleyCallback() sintax
  • Esdras de Morais da Silva
    How is possible implementing the sintax: GetVolleyResponse.VolleyCallback()?
  • Esdras de Morais da Silva
    My compilationg return me the following error to sintax “GetVolleyResponse.VolleyCallback()”:

    error: cannot find symbol class VolleyCallback

  • Murray
    This is exactly what I needed. Why isn’t this more widely used? Note that to be perfect, you really should explain/correct the following:
    – TOKEN – never defined in your code
    – GetVolleyResponse (I assume you put your getResponse method inside a GetVolleyResponse class)
    – change GET to POST to match your title
    – how to process the parameters (from jsonValue)

    I had some trouble with contexts because I’m calling it getResponse from within my Utils class, but I think I’ve found my way through.

    • anchit (Moderator)

      Thanks for your suggestions.

      We are glad that this article could help you in some way.

  • 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