Ok.. this is totally weird. I'm trying to make a http request in android. It works and I can 'build' a string using StringWriter but the code throws an exception at writer.toString(). It makes no difference if I use StringBuilder or StringWriter.
try {
HttpResponse response = httpClient.execute(httpPost);
InputStream is = response.getEntity().getContent();
StringBuilder writer = new StringBuilder();
//StringWriter writer = new StringWriter();
int ch;
while(( ch = is.read()) != -1) {
writer.append((char)ch);
}
String theString = writer.toString();
return theString;
}
catch (Exception e) {
return e.getMessage();
}
Any ideas?
The problem is that your HTTP thread is attempting to update the UI Thread with the HTTPResponse
in a TextView
or other such UI element so a CalledFromWrongThreadException
is being raised.
Using a Handler
or other such mechanism to redirect to the UI Thread for the UI update will solve your problem.