-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StrictMode error - resource leak on 1.4.9 #963
Comments
What is it with you guys, always closing and reopening the issues immediately? Anyway, I looked into this, and DefaultHttpClient in upstream version we do use, is extending AbstractHttpClient which is extending CloseableHttpClient So the solution is to call Thank you for report! |
Where I have to call .close() ? I call client.post in a Asynctask, in onPostexecute. It happens when Android remove the Asynctask. Best regards. |
With SDK 22 and 1.4.8 Work without problems... |
Close should be called when the original thread finishes, so the workaround is to init the AsyncHttpClient in ApplicationContext and call close() when application finishes. If it's created only within AsyncTask, you should call close() in onPostExecute Yes, this is issue introduced by 1.4.9 version |
AsyncHttpClient client = new AsyncHttpClient(); and how init AsyncHttpclient in application context? Thank you very much. |
client.getHttpClient().close(); To init it within ApplicationContext, create instance in Application subclass and use it either statically ( Regarding the second question, please consult Android Developer documentation and internet, you should know these things already. |
I tried before client.getHttpClient().close(); but it doesn't exits! |
Sorry this is ugly Returned is only of type HttpClient (only interface), however internally the type is DefaultHttpClient, which does have the method |
Perfect, I use ((DefaultHttpClient)client.getHttpClient()).close(); OnSuccess and OnFailure. Everything Works ok now. Thank you very much again! |
You're welcome |
Sorry again, but in Android 4.4.2 I receive this error:
when I close the Httpclient ((DefaultHttpClient) client.getHttpClient()).close(); |
Then do it in |
java.lang.IllegalArgumentException: Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead. |
You're using the library wrong way, try to figure it out somehow. Also when you post errors, post a full stacktrace and relevant code please, I'm not going to guess what is the correct solution from your vaguely described situation. |
I follow with your suggestions but give me another errors again and again. All problem started with the resource leak. Anyway, this seems to work:
Thank you very much again. I hope this helps to someone. |
Disabling StrictMode is not helpfull, we use it as testing condition in our sample application. Also I don't see any reason why you're using AsyncHttpClient within AsyncTask, it'd be easier to use directly HttpClient in there, since you're already on different thread. |
I use AsyncTask to get the data from Database and in onPostExecute I call AsyncHttpClient. You told me call AsyncHttpClient on doInBackground ... Anyway I call in other places AsyncHttpClient without AsyncHttpClient and give me the same error. That is how I have to call .close() new Thread(new Runnable() {public void run() {((DefaultHttpClient) client.getHttpClient()).close();}}).start(); |
This library purpose is to provide seamless access to networking from Main Thread (UI Thread) from Activities, Fragments, Widgets, Views, ... So I guess the best in context of Activity would be to create instance of AsyncHttpClient in Activity Regarding the AsyncTask, if you do call Yes, putting the Sample task to close the client could be pretty simple, something like this: class CloseHttpClientTask extends AsyncTask<HttpClient, Void, Void> {
@Override
protected Void doInBackground(HttpClient... params) {
for(HttpClient hc : params){
if(hc != null && hc instanceof Closeable){
try {
((Closeable) hc).close();
}catch(Throwable ignored){
}
}
}
return null;
}
} Add logging if you need to. new CloseHttpClientTask().execute(client.getHttpClient()); Which is probably bit more pretty than using raw Thread and Runnable |
Thank you very much, I will take in account your suggestion when open and close the HttpClient. |
Glad to help you out! |
The text was updated successfully, but these errors were encountered: