How To Remove Fatal Exception: Asynctask In Android
10-23 00:41:00.705: E/AndroidRuntime(3622): FATAL EXCEPTION: AsyncTask #1 10-23 00:41:00.705: E/AndroidRuntime(3622): java.lang.RuntimeException: An error occured while executing d
Solution 1:
The reason is you can only consume Content
from Entity
once.
You did it twice (maybe without you knowing it) in here
Readerreader=newInputStreamReader(response.getEntity().getContent());
and here
response.getEntity().writeTo(out);
I know this is sounds a little weird but actually the writeTo() function will get content from the entity to write to the OutputStream
. You can see it in the documentation here
Another workaround you can use is turning it to string and let GSON handle it
@Overrideprotected String doInBackground(String... uri) {
HttpClienthttpclient=newDefaultHttpClient();
HttpResponse response;
StringresponseString=null;
try {
response = httpclient.execute(newHttpGet(uri[0]));
StatusLinestatusLine= response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
responseString = EntityUtils.toString(response.getEntity()); // content will be consume only onceGsongson=newGson();
Holderresponse1= gson.fromJson(responseString, Holder.class);
} else{
//Closes the connection.
response.getEntity().getContent().close();
thrownewIOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..\
progressDialog.hide();
} catch (IOException e) {
//TODO Handle problems..
progressDialog.hide();
}
return responseString;
}
I hope my answer can help you!
Post a Comment for "How To Remove Fatal Exception: Asynctask In Android"