2017-02-28 118 views
0

我使用rx java与Retrofit,okhttp。 下载内容,而读取输入sream收到“套接字关闭”套接字关闭异常android

试图用像不同的方式后: *正从onNext方法,然后读取输入流的响应

我的代码如下所示:

mApi.getPdf(mUserManager.getUserProfile().getAuthToken()) 
      .compose(mRxUtils.applySchedulers()) 
      .subscribe(new Subscriber<ResponseBody>() { 


       @Override 
       public void onCompleted() { 

       } 

       @Override 
       public void onError(Throwable e) { 
        mApiErrorManager.handleError(e); 
       } 

       @Override 
       public void onNext(ResponseBody downloadPDFModelResponse) { 


        downloadFile(downloadPDFModelResponse, myFile); 

       } 
      }); 

下载功能如下:

private void downloadFile(ResponseBody body, File myFile) throws IOException { 

    int count; 
    byte data[] = new byte[1024 * 4]; 
    long fileSize = body.contentLength(); 
    InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8); 
    File outputFile = myFile; 
    OutputStream output = new FileOutputStream(outputFile); 
    long total = 0; 
    long startTime = System.currentTimeMillis(); 
    //int timeCount = 1; 
    while ((count = bis.read(data)) != -1) { //here getting Socket closed exception 

     total += count; 
     int progress = (int) ((total * 100)/fileSize); 

     mEventBus.post(new DownloadProgress(progress)); 

     output.write(data, 0, count); 
    } 
    mEventBus.post(new DownloadProgress(DownloadProgress.COMPLETE)); 
    output.flush(); 
    output.close(); 
    bis.close(); 


} 
+0

你所说的“读输入流”意思?你的输入流是什么?你遇到了什么错误? – degs

+0

你可以用你的logcat发布一些代码吗? – JediBurrell

+0

可能这会帮助你: [你的ans](http://stackoverflow.com/a/8840042) –

回答