B
B
Benderlidze2014-01-28 11:07:40
Android
Benderlidze, 2014-01-28 11:07:40

File download and parsing in android?

Good afternoon,
in the application you need to parse a small xml file.
I do this - I download a file (AsyncTask) to my phone, then I try to parse it right there in the general stream.
Almost always there is a situation - it looks like the file has not been downloaded yet, and the parser parses an empty file.
Those. it turns out that it is also necessary to parse in the asynctask, or in the general thread, wait for the asynctask to work and only then start the parser?
Another question about the cache is just copying files or is there a special android tool.
Thank you.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mousexaker, 2014-01-28
@mousexaker

There are several approaches to solve this problem:
1. Download XML to a file and then parse this file.
2. Download XML into memory and parse it from memory (bad option, since XML size can be large).
Both options should be done in AsyncTask. The parsing process can be performed in the same AsyncTask or in another one, it depends on the task at hand. I do not recommend parsing in the main thread, since this operation is quite laborious, especially if the XML is large.

S
senneco, 2014-01-28
@senneco

There was a feeling that you are starting the parsing not after the completion of the asynchronous request, but after the start of the task execution, right in the main thread.
If so, then you need to start parsing in the onPostExecute method of your AsyncTask. And whether to parse XML asynchronously, or directly on the main thread is another question, but it’s better, of course, to do it in AsyncTask.
And about the cache - specify your question, what exactly do you mean? If at all about caching methods, then the answer will be: as such, super-universal specials. no caching facilities. You can use ContentProvide, DB, Shared preference and just store the file in the system - it already depends on the specific task.

M
mousexaker, 2014-01-28
@mousexaker

If I'm not mistaken, the onPostExecute method is executed in the main thread and not in the background.

B
Benderlidze, 2014-01-28
@Benderlidze

I inserted the asynctask get() method immediately after the task was called, and everything magically worked as it should,
although at the moment I don’t understand why.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question