I'm having thoughts abut my current app. I have this TabHost class with two tabs. Tab1 shows data from a WS in a Listview. Tab2 shows the same data as a diagram in a webview.
Of course, I'd rather not call the same webservice for the same data twice, so I was thinking of doing the WS call already in the TabHost class and t开发者_如何转开发hen send the data as a JSONArray to both my tabs using putExtra.
Is that possible or simply stupid? Regards
Have a look at this video that talks about making WebService calls and RESTful clients.
As a general principle it is a bad idea to make a WS call from a Activity or the main UI thread. There is a high probability of getting ANR's (Application Not Responding) when the network is slow or if the webService is taking time to respond.
JSON is meant for making WebService calls and usage should be restricted to that layer. While nothing stops you from using JSON for passing data I would consider it as an anti pattern or hack.
Suggested way to do this.
Put your webservice into an IntentService class (This will ensure the UI thread does not block).
Convert the response into a Parcelable object and send it back to the activity via an intent.
You can use this Parcelable object in the intent.putExtra to pass it around.
While this sounds like a lot of work it is cleaner and will save you a lot of pain later. Also the documentation on IntentService and Parcelable is a bit terse so you will have to do some searching on StackOverflow to get it right.
精彩评论