Skip to content
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

HttpURLConnection is opened but never closed #743

Open
duongphuhiep opened this issue Sep 7, 2014 · 2 comments
Open

HttpURLConnection is opened but never closed #743

duongphuhiep opened this issue Sep 7, 2014 · 2 comments
Labels

Comments

@duongphuhiep
Copy link

I think it is related to the code design

09-07 23:26:54.969  25821-25830/dh.newspaper E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'end' not called
            at dalvik.system.CloseGuard.open(CloseGuard.java:184)
            at java.util.zip.Inflater.<init>(Inflater.java:82)
            at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:96)
            at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
            at com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468)
            at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:114)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:85)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:290)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisk(LoadAndDisplayImageTask.java:273)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:229)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:135)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

Checkout the following codes:
We can returned the InputStream and still close the connection.

public static InputStream getStreamFromUrl(String address, String userAgent, ICancellation cancelListener) throws IOException {
        byte[] content = downloadContent(address, userAgent, cancelListener);
        return content==null ? null : new ByteArrayInputStream(content);
    }
public static byte[] downloadContent(String address, String userAgent, ICancellation cancelListener) throws IOException {
        Stopwatch sw = Stopwatch.createStarted();

        try {

            HttpURLConnection httpConnection = okHttpClient.open(new URL(address));
            httpConnection.addRequestProperty("User-Agent", userAgent);
            int responseCode = httpConnection.getResponseCode();

            if (200 <= responseCode && responseCode < 300) {
                InputStream input = httpConnection.getInputStream();
                ByteArrayOutputStream baos = null;
                try {
                    //download all the page to other InputStream
                    baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = input.read(buffer)) > -1) {
                        ThreadUtils.checkCancellation(cancelListener, "Download HC canceled (" + sw.elapsed(TimeUnit.MILLISECONDS) + " ms) " + address);
                        baos.write(buffer, 0, len);
                    }
                    baos.flush();

                    byte[] content = baos.toByteArray();
                    baos.close();
                    Log.v(TAG, "Download HC end " + content.length + " bytes (" + sw.elapsed(TimeUnit.MILLISECONDS) + " ms) " + address);
                    return content;
                } finally {
                    if (input != null) {
                        input.close();
                    }
                    if (baos != null) {
                        baos.close();
                    }
                }
            } else {
                throw new IllegalStateException("Failed to connect to " + address + ": " + httpConnection.getResponseMessage() + " (" + responseCode + ")");
            }
        }
        catch (ConnectException e) {
            Log.w(TAG, "Download HC error (" + sw.elapsed(TimeUnit.MILLISECONDS) + " ms) " + address + " : "+e);
            throw e;
        }
    }

If you donnot want to immediatly download the content like this, I think that there should have other code pattern to properly close the connection.

@nostra13
Copy link
Owner

nostra13 commented Sep 7, 2014

I don't quite understand what you proposed.

@nostra13 nostra13 added the Bug label Sep 7, 2014
@Neuifo
Copy link

Neuifo commented Oct 15, 2014

I have find a question in your BaseImageDownloader with HttpURLConnection(though it is not happend frequently) It happend when you use a re-directed Url to load a Image,the first time the image will show success,the second time will failed,and the thired time it will be ok,it's very interesting and annoying,
all you need is to add a request head
conn.setRequestProperty("Connection", "close");
because the second time the request code was -1 not 200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants