2017-02-22 124 views
0

上传视频时,我使用此代码,以我的视频上传到改造服务器ETIMEDOUT(连接超时)使用改装2

private String uploadVideoToServer(String pathToVideoFile) { 
    Log.v("test_get", "get the file"); 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("http://xxx.xxx.xxx.xxx:xxxx/") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    SmileVideoAPI service = retrofit.create(SmileVideoAPI.class); 
    MediaType MEDIA_TYPE = MediaType.parse("multipart/form-data"); 
    File videoFile = new File(pathToVideoFile); 
    //RequestBody videoBody = RequestBody.create(MEDIA_TYPE, videoFile); 
    ProgressRequestBody videoBody = new ProgressRequestBody(videoFile, this); 
    MultipartBody.Part vFile = MultipartBody.Part.createFormData("file", videoFile.getName(), videoBody); 
    RequestBody description = createPartFromString("desc"); 
    Log.v("test_get", "before uploading"); 
    Call<ResponseBody> call = service.uploadVideo(description, vFile); 
    Log.v("test_get", "after uploading"); 
    call.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
      if (response.isSuccessful()) { 
       Log.i("mok", "S"); 
       ResponseBody rb = response.body(); 
       Log.i("mok", rb.toString()); 
       mProgress.setProgress(100); 
       Intent intent = new Intent(UploadActivity.this, UploadCompleteActivity.class); 
       startActivity(intent); 
       finish(); 
      } else { 
       Log.i("mok", "F"); 
       ResponseBody rb = response.errorBody(); 
       Log.i("mok", rb.toString()); 
      } 
     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable t) { 
      t.printStackTrace(); 
      Log.i("mok", t.getCause() + ""); 
      Log.i("mok", "T"); 
      Toast.makeText(getApplicationContext(), "Upload fail", Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(UploadActivity.this, MainActivity.class); 
      startActivity(intent); 
      finish(); 
     } 
    }); 
    return msg; 
} 

它载的连接设置第一时间的视频,但是,不时抛出下面的错误。

libcore.io.ErrnoException:isConnected失败:ETIMEDOUT(连接超时)

任何人都可以解释我为什么会这样,怎么能解决呢?现在,我正在研究如何关闭我的连接,因为我怀疑这可能是因为未关闭连接。

回答

0

这是造成,因为连接不被释放, 我通过把一个

response.body().close(); 
解决了这个
相关问题