Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchMethodError: libcore.io.IoUtils.closeQuietly when using URLConnection

I get NoSuchMethodError: libcore.io.IoUtils.closeQuietly when I use URLConnectionon android 2.3.x

I use URLConnection to save file locally and use cache.

My app should run on android 2.3.x but compiled with API17 because of some AndroidManifest.xml defs.

I use JRE7. Maybe downgrading to JRE6 would help?

Any what causes it or how to solve it?

My code:

    public synchronized boolean saveFileLocally(String strUrl, String fileName) {
    boolean res = false;
    if (strUrl == null || fileName == null)
        return res;

    URL url = null;
    InputStream input = null;
    OutputStream output = null;
    try {
        url = new URL(strUrl);
        URLConnection connection;
        connection = url.openConnection();
        connection.setUseCaches(true);
        connection.connect();
        input = connection.getInputStream(); // HERE I GET THE EXCEPTION

        byte[] buffer = getTempBitmapStorage();
        output = new FileOutputStream(fileName);

        int bytesRead = 0;
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
            output.write(buffer, 0, bytesRead);
        }

        res = true;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return res;
}

Logcat:

 java.lang.NoSuchMethodError: libcore.io.IoUtils.closeQuietly
        at libcore.net.http.HttpConnection.closeSocketAndStreams(HttpConnection.java:136)
        at libcore.net.http.HttpEngine.release(HttpEngine.java:517)
        at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
        at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:181)
        at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:273)
        at com.aa.android.utilslib.web.WebHelper.saveFileLocally(WebHelper.java:632)
        at com.bb.android.GCMIntentService.handleReceivedData(GCMIntentService.java:155)
        at com.bb.android.GCMIntentService.onMessage(GCMIntentService.java:81)
        at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:130)
        at android.os.HandlerThread.run(HandlerThread.java:60)
like image 819
Asaf Pinhassi Avatar asked Jan 31 '26 00:01

Asaf Pinhassi


1 Answers

See your error in locat and go to this link here:

public void closeSocketAndStreams() {
    IoUtils.closeQuietly(sslOutputStream);
    IoUtils.closeQuietly(sslInputStream);
    IoUtils.closeQuietly(sslSocket);
    IoUtils.closeQuietly(outputStream);
    IoUtils.closeQuietly(inputStream);
    IoUtils.closeQuietly(socket);
}

line 136 is: IoUtils.closeQuietly(inputStream);

We can see this method call IoUtils.closeQuietly with a InputStream parameter.

But when you see here to view IoUtils class

You can not find closeQuietly with a InputStream parameter. So, I think this is the cause of error. You should find another library or under version.

My english is not good. I'm sorry for the inconvenience caused to you

like image 91
Ba Tới Xì Cơ Avatar answered Feb 01 '26 14:02

Ba Tới Xì Cơ