我在 android 2.3.x 上NoSuchMethodError: libcore.io.IoUtils.closeQuietly
使用时得到URLConnection
我使用 URLConnection 在本地保存文件并使用缓存。
我的应用程序应该在 android 2.3.x 上运行,但由于一些 AndroidManifest.xml 定义而使用 API17 编译。
我使用 JRE7。也许降级到 JRE6 会有所帮助?
有什么原因导致它或如何解决它?
我的代码:
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;
}
日志猫:
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)