【问题标题】:ETIMEDOUT (Connection timed out) when uploading video using retrofit 2使用改造 2 上传视频时的 ETIMEDOUT(连接超时)
【发布时间】:2017-02-22 05:56:52
【问题描述】:

我正在使用此代码将我的视频上传到改造服务器

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 failed: ETIMEDOUT(连接超时)

谁能解释一下为什么会发生这种情况,我该如何解决?现在,我正在研究如何关闭连接的解决方案,因为我怀疑这可能是因为连接未关闭。

【问题讨论】:

    标签: java file-upload retrofit2


    【解决方案1】:

    这是因为连接没有释放造成的, 我通过放置一个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      相关资源
      最近更新 更多