Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley and processing data returned in background on Android

I have some troubles finding some informations about Volley and the response.Listener.

Basically I have an operation to ask data to my backend, call is made throughout Volley in background (Handled by Volley itself) meanwhile it call the onResponse on the main thread.

Do I need to do a runnable by myself to process theses datas in background or there is a way to force the onResponse to run in background ?

Thank you.

EDIT :

Here is the code I'm running then.

private Response.Listener<String> volleyResp = new Response.Listener<String>() {
    @override
    public void onResponse(final String jsonResp) {
        new Thread() {
            public void run() {
                // Do something ... Insert in DB for example.
            }
        }.start();
    }
like image 418
Atheryl Avatar asked Mar 20 '14 03:03

Atheryl


People also ask

Does volley run in background thread?

Every network request performed by Volley is performed in a background thread. Volley takes care of this behind the scenes. So there is no need to perform a request on a different thread, since that's already happening.

What is Volley used for in Android?

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley offers the following benefits: Automatic scheduling of network requests. Multiple concurrent network connections.

What is Android background task?

Definition of background work. An app is running in the background when both the following conditions are satisfied: None of the app's activities are currently visible to the user. The app isn't running any foreground services that started while an activity from the app was visible to the user.


1 Answers

Please read my reply until the end

If you want to 'process' data in the same thread as the request, you should have subclasses of Request (or JsonRequest) and implements parseNetworkResponse(NetworkResponse response)

Example :


public class TotoRequest extends JsonRequest {
    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString =
                    new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            JSONObject jsonObject = new JSONObject(jsonString);
            // Process data here
        } catch (UnsupportedEncodingException | JSONException e) {
            return Response.error(new ParseError(e));
        }
    }
}

This is the reply to your question but I think it's not what you actually want.

It's probably safer to either process your data in the main thread if your data is small.

If your data is important, or if you want to insert/update it in a database you should simply continue doing what you are doing.

Also, I would recommend you to use an AsyncTask instead of a thread.

like image 102
Xval Avatar answered Sep 28 '22 07:09

Xval