AndyAndroid
AndyAndroid

Reputation: 4069

android async task: Only the original thread that created a view hierarchy can touch its views

I have an async task in doInBackround I do all kind of stuff, after various sections I run a

 onProgressUpdate("You proceeded a bit further 1/5");

That works fine a multiple times until (still all in doInBackground) I have

HttpSessionToken = (HttpURLConnection)new URL("http://myserver").openConnection();
HttpSessionToken.setRequestMethod("GET");   
HttpSessionToken.setRequestProperty("Accept", "application/json");

onProgressUpdate("Still everything is working fine");
int returnCode = HttpSessionToken.getResponseCode();
onProgressUpdate("This onProgressUpdate crashes!");

so the last onProgressUpdate crashes, but why does it crash? Did the getResponseCode() switch the thread I am running on?

Upvotes: 2

Views: 1099

Answers (1)

Budius
Budius

Reputation: 39836

you don't call directly the onProgressUpdate, you have to call publishProgress and let the AsynTask framework to handle the onProgressUpdate to be called back on the UI thread.

Upvotes: 3

Related Questions