Volleyball and handling data returned in the background on Android

I have some problems finding some information about Volleyle and the answer. Sheet.

Basically, I have an operation to request data on my backend, the call is made in the entire volleyball in the background (processed by the volleyball itself), meanwhile it calls onResponse in the main thread.

Do I need to do runnable on my own to process abstract data in the background or is there a way to get onResponse to work in the background?

Thanks.

EDIT:

Here is the code that I run 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(); } 
+7
android multithreading android-volley
source share
1 answer

Read my answer to the end

If you want to process data in the same stream as the request, you must have subclasses of Request (or JsonRequest) and implement 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 answer to your question, but I think this is not what you really want.

It is probably safer to 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 the database, you should simply continue to do what you are doing.

In addition, I would recommend that you use AsyncTask instead of stream.

+5
source share

All Articles