How to Return Value from Function Onresponse of Volley

How can I return value from function onResponse of Volley?

You want to use callback interfaces like so:

public void getString(final VolleyCallback callback) {
StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
... // (optionally) some manipulation of the response
callback.onSuccess(response);
}
}...
}}

Where the callback is defined as

public interface VolleyCallback{
void onSuccess(String result);
}

Example code inside activity:

public void onResume(){
super.onResume();

getString(new VolleyCallback(){
@Override
public void onSuccess(String result){
... //do stuff here
}
});
}

You can also make VolleyCallback more robust, using generic types if you want to do processing, or adding start(), failed(Exception e), complete(), etc methods to do a little more fine-grained state checking.

Keep in mind this is an async call, so you will have to update views, etc when you get the result back (inside success()).

Android Volley return value from response

As it was mentioned, you need to implement interface and get a callback

Create a new interface like this

public interface GeneralCallbacks {
void VolleyResponse(String data);
}

Implement the interface on your activity class

public class YourActivity implements ScoreboardCallback
{
@Override
public void VolleyResponse(String data)
{
//do stuff with data
}
}

And finally changing your response function to something like this

        @Override
public void onResponse(String response) {
((GeneralCallbacks)context).VolleyResponse(response); // This will make a callback to activity.
aPerson.SomeFunction(); // This will call person's function
Log.d("RESPONSE",response.toString());
}

How to callback a value from Volley On Response function in Kotlin

You can't, the code you have executes asynchronously so you can't have the return statement there.
But it doesn't mean you can not get the value. The easiest option is create a LiveData<Boolean> and observe it in your activity. When the value changes you will get notified.

The next thing you can do is you can create an interface.

interface MyCallback{
fun onValueChanged()
}

Then implement this interface in your activity and override the method. Then you can use it to get a callback when your asynchronous call finishes. See the below code.

interface MyInterface{
fun onCallback(response:Boolean)
}

class LoginActivity : AppCompatActivity(), AuthListener, MyInterface {

val myInterface = this

override fun onCallback(response: Boolean) {

}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val queue = Volley.newRequestQueue(activity)
val req = JsonObjectRequest(Request.Method.POST, url, jsonObj,
Response.Listener { response ->
val JSONObj = response.getString("Status")
if(JSONObj=="200"){
//return true
myInterface.onCallback(true)
}
else{
myInterface.onCallback(false)
}
}, Response.ErrorListener {
// return false
}
)
queue.add(req)

}
}

Hope this helps. Thank You :)



Related Topics



Leave a reply



Submit