Reputation: 11177
I just noticed Gson performances are not what I expected and some parsing processes are taking a lot of time. I'm not looking for another solution, my app is quite big and I don't want to alter its structure with something else like jackson.
My Android app allows the user to stop an http request with the back key. My code abort the request and basically stops the task. The thing is, it needs to wait for the method gson.fromJson(result)
to abort the task. That instruction can last for a couple of seconds.
Is there any way to tell the gson object to abort the parsing process?
Upvotes: 0
Views: 669
Reputation: 116522
You can probably do this by buffering response, and only doing parsing when everything is complete; or, stopping handling is cancelled.
Upvotes: 0
Reputation: 80340
AFAIK `Gson.fromJson()’Is a blocking method call which in Java can not be stopped.
Run parsing in the background thread. In this case youn can just leave it to run as it does not affect the UI part of your app.
Use AsynTask for this. Add a field cancelled
to notify the task that it was cancelled so that it does not update UI at the end.
Upvotes: 0
Reputation: 2756
I am not familiar with GSON but you could close the underlying stream of JSON data (Inpustream) to end the parsing process with an exception, which you could catch. That should work in case the input data can be a Stream. I have just taken a look at the GSON API and the fromJson method takes a Reader parameter, which can be closed. A bad way could be also to run it in a thread and call thread.stop().
Upvotes: 2
Reputation: 8787
Maybe you could try to split the single gson call to several subcalls (since the parsed data seems to be huge when the request performs for seconds).
Basically I mean to parse every array element on it's own or parse attributes of class one by one instead doing the complete work in a single call. So you could interrupt that process more easily.
Upvotes: 0