Android NoSuchMethodError:使用URLConnection时libcore.io.IoUtils.closequiely
在android 2.3.x上使用Android NoSuchMethodError:使用URLConnection时libcore.io.IoUtils.closequiely,android,inputstream,urlconnection,Android,Inputstream,Urlconnection,在android 2.3.x上使用URLConnection时,我得到NoSuchMethodError:libcore.io.IoUtils.closequity 我使用URLConnection在本地保存文件并使用缓存 我的应用程序应该运行在android 2.3.x上,但由于一些AndroidManifest.xml定义,因此使用API17编译 我使用JRE7。也许降级到JRE6会有所帮助 是什么原因造成的,或者如何解决 我的代码: public synchronized bool
URLConnection
时,我得到NoSuchMethodError:libcore.io.IoUtils.closequity
我使用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)
查看locat中的错误并转到以下链接: 第136行是:IoUtils.closequilly(inputStream) 我们可以看到此方法使用InputStream参数调用IoUtils.close 但当您看到查看IoUtils类时 您无法使用InputStream参数找到closequity。所以,我认为这是错误的原因。您应该找到另一个库或版本
我的英语不好。很抱歉给您带来不便您如何让HttpsURLConnectionImpl打电话给IoUtils.close?您是否有可能包含HttpResponseCache库的外部版本并从源代码构建该库?如果是这样的话,那可能就是导致您的问题的原因(您应该将它作为一个罐子包含在内) 出于好奇,如果你比较姜饼的来源,比如说4.3分
您将看到姜饼缺少closequiety(Socket)的实现。但是,无论您使用的是哪个版本的HttpsURLConnectionImpl,您都希望得到不同版本的IoUtils,它可能来自特定HttpsURLConnectionImpl所来自的同一个地方。看起来“libcore.io.IoUtils”丢失了-在android 2.3.x中可能无法保存-您确保它可以保存吗?可能是手动导入修复了吗?缺少关闭过程。我想它在2.3.x上不可用,但我能做些什么呢?我不直接使用它。libcore.net.http.HttpsURLConnectionImpl.getInputStream使用它…我建议包括最新的libcore库,这样你就可以确保没有遗漏任何东西。(向后兼容可以减轻痛苦。)我可能在这里错了,但是
InputStream
实现了Closeable
,这是一个自动关闭的和IoUtils.closequity(可自动关闭)
不存在。
public void closeSocketAndStreams() {
IoUtils.closeQuietly(sslOutputStream);
IoUtils.closeQuietly(sslInputStream);
IoUtils.closeQuietly(sslSocket);
IoUtils.closeQuietly(outputStream);
IoUtils.closeQuietly(inputStream);
IoUtils.closeQuietly(socket);
}