Android OkHttp InputStream java.IOException.closed
我使用Android OkHttp InputStream java.IOException.closed,java,android,inputstream,okhttp,Java,Android,Inputstream,Okhttp,我使用OkHttpClient在服务器上下载一个数据库,并在我的Android应用程序上复制它,请求是可以的,我得到了好的内容 然而,当我试图将byteStream写入文件时,我得到了一个java.IOException.closed。你知道我做错了什么吗 Response httpResponse = webApiClient.execute( new WebApiRequest(WebApiMethod.DB_DOWNLOAD), context); if (httpRespons
OkHttpClient
在服务器上下载一个数据库,并在我的Android应用程序上复制它,请求是可以的,我得到了好的内容
然而,当我试图将byteStream
写入文件时,我得到了一个java.IOException.closed
。你知道我做错了什么吗
Response httpResponse = webApiClient.execute(
new WebApiRequest(WebApiMethod.DB_DOWNLOAD), context);
if (httpResponse.code() == 200)
{
try
{
InputStream inputStream = httpResponse.body().byteStream();
File databasePath = context.getDatabasePath(Constant.DATABASE_NAME);
FileOutputStream output = new FileOutputStream(databasePath);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len;
while ((len = inputStream.read(buffer)) != -1)
{
output.write(buffer, 0, len);
}
success = true;
}
catch (Exception exc)
{
Utils.DisplayException(exc, context);
}
}
我还试图用一个BufferedSink
读取我的ByteStream
,但结果是一样的
BufferedSink sink = Okio.buffer(Okio.sink(databasePath));
sink.writeAll(httpResponse.body().source());
sink.close();
堆栈跟踪:
java.io.IOException: closed
at okio.RealBufferedSource$1.read(RealBufferedSource.java:367)
at java.io.InputStream.read(InputStream.java:162)
at com.org.dbconn.LoginActivity$DbDownloadTask.doInBackground(LoginActivity.java:475)
at com.org.dbconn.LoginActivity$DbDownloadTask.doInBackground(LoginActivity.java:436)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
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:818)
我找到了流被关闭的原因,对于一些测试,我用
System.out.println
检查了输出字符串的内容,但是当您读取或打印输出时,它会自动关闭流,这就是我出错的原因
总之,,
流需要记住一件事:读取或打印输出流将关闭它。请发布完整的堆栈跟踪,好吗?您是否使用了任何可能关闭流的拦截器?您刚刚救了我一命。mahn:D。谢谢。