I'm trying to access a server so I can receive a JSON String. But apparently in Ice Cream Sandwich you can't do network operations in the main thread, but the AsyncTask class is confusing me and not working. Here's what I have so far:
//up in main code
Void blah = null;
URI uri = "kubie.dyndns-home.com/R2Bar2/ingredients.php";
new DownloadFilesTask().execute(uri , blah, blah);
private class DownloadFilesTask extends AsyncTask<URI, Void, Void> {
protected Void doInBackground(URI... uri) {
HttpClient client = new DefaultHttpClient();
String json = "";
int duration = Toast.LENGTH_SHORT;
try {
HttpResponse response = null;
BufferedReader rd = null;
String line = "";
HttpGet request = new HttpGet(uri);
} catch (URISyntaxException e1) {
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
}
It's not liking my HttpGet request = new HttpGet(uri)
It says to change uri to URI, but it already is!
I've tried changing all parameters to Void, but my app just force closes.
Anyone know how to do this?
Try this example:
The last one, your server returns php file, is it right behaviour?
You can, but it will give you a warning in LogCat. And you should not do network operations on the main application thread, as it will freeze your UI and possibly give you an "Application Not Responding" (ANR) dialog.
In your current code, you are not actually executing the
HttpGet
request.No, it is not.
The
...
afterURI
in yourdoInBackground()
declaration means that this method accepts a variable number of arguments. Youruri
parameter is, in effect, aURI[]
. If you are only ever callingexecute()
with oneURI
, you get at thatURI
viauri[0]
, noturi
.