Ich benutze eine OkHttpClient
, um eine Datenbank auf dem Server herunterzuladen und auf meine Android-Anwendung zu kopieren, ist die Anfrage in Ordnung und ich bekomme den guten Inhalt. Wenn ich versuche, meine byteStream
in meine Datei zu schreiben, bekomme ich eine . Weißt du, was ich falsch mache?Android OkHttp InputStream 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);
}
}
Ich habe auch versucht, meine lesen ByteStream
mit einem BufferedSink
aber war das Ergebnis das gleiche
BufferedSink sink = Okio.buffer(Okio.sink(databasePath));
sink.writeAll(httpResponse.body().source());
sink.close();
Stacktrace:
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)
Könnten Sie bitte Ihre volle Stapelverfolgung posten? – apmartin1991
Verwenden Sie Interzeptoren, die den Stream schließen könnten? –