How to use HttpClient (or other HTTP class) to retrieve JSON asynchronously
I've got the content from a HttpClient, but I'm not sure where to go next, in order to parse my JSON result. Also, how do I do this asynchronously so that I can display a wait dialog to the user with the option of cancelling the current request (not bothered about the UI example, how do I setup the HTTP class to be cancellable)?
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://mysite/test.json");
开发者_运维技巧try
{
HttpResponse response = client.execute(request);
// What if I want to cancel now??
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
}
Android has a class specifically for this sort of thing: AsyncTask.
See the answer to this question: How to create Http Connection using AsyncTask class?
I'm not a Android programmer but I imagine you need to start a thread.
http://download.oracle.com/javase/tutorial/essential/concurrency/
精彩评论