Reputation: 843
My app requires to do several http connections before it can provide a result which is further used in the app. I need to use a webview in the process to open a link and get/parse the resulting html. Where should I post the code for these connections and the webview? Note that the intermediate steps are not relevant to the user, only if it fails somewhere.
At the moment, I have 2 activities: first one does the first connection (in asynctask) which forms the URL which needs to be opened in a webview. I open the 2nd activity passing the url, where i open the url in the webview, get the html, and do another connection for the final result.
I thought of the following options: merge these 2 activities and use a hidden webview? Maybe put all of it in a no-UI fragment? Or code it as a service?
I don't want the app to crash/freeze if e.g. the internet is veery slow or there are some issues with the connection.
Upvotes: 0
Views: 189
Reputation: 843
In the end, I moved things around and ended up still with 2 activities but with the 2nd one containing just a webview. So from 1st activity, I startActivityForResult
twice (since I need to use the webview twice) ending back in 1st activity. In other words, the process goes as following:
Act1 (asyncTask for one http job returning a link) -> Act2 (open the link in webview and return data) -> Act1 (use that data to get 2nd link) -> Act2 (start it again to get final result and pass it back) -> Act1.
It is certainly not elegant but it seems like the simplest solution. Guess a fragment would be more appropriate but they're rather complicated for my skills right now.
Upvotes: 0
Reputation: 11
If you just need to get data from a server (like a REST api), then you should make an HTTP request using a library like OKHTTP. The network request will be async as a requirement, so it won't affect the UI. You'll have to wait for the responses to your requests. Once you have the responses, parse them. You'll need logic in you app to prevent access features until you have all of your responses.
Upvotes: 1