2017-08-07 1675 views
0

我使用okhttp将文件上传到我的服务器,并将文件加载到Google文档查看器中供我的用户预览。但是,由于我无法在服务器上找到文件,因此上传失败。引发的异常提供以下堆栈跟踪:java.net.SocketException:连接重置OKHTTP

Cannot setInForeground() - never saw a connection for the pid: 22551 
08-08 04:23:37.208 22551-22605/ W/System.err: java.net.SocketException: Connection reset 
08-08 04:23:37.330 22551-22551/ W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551 
08-08 04:23:37.331 22551-22551/ W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551 
08-08 04:23:39.213 22551-22551/ W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551 
08-08 04:23:40.672 22551-22551/ W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551 
08-08 04:23:41.203 22551-22551/ W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 22551 
08-08 04:23:52.103 2751-3347/? D/WifiQualifiedNetworkSelector:: Current connection state (true,false,false) 
08-08 04:24:04.811 4124-4124/? W/GmsClientEvents: unregisterConnectionCallbacks(): listener [email protected] not found 

PORT 80也是开放的。但是,有时上传失败一致。大多数时候它工作正常。这是服务器端还是客户端问题?如果客户端是它的原因,它是如何工作的某些时候。超时错误是问题的原因吗?我检查了大或小的文件,但我不知道是什么导致了这个问题。

这是我上传的文件代码:在计算器其他问题讨论

try { 
     File f = new File(selectedFilePath); 

     String file_path = f.getAbsolutePath(); 
     OkHttpClient client = new OkHttpClient(); 

     RequestBody file_body = RequestBody.create(MediaType.parse(content_type),f); 
     Log.e("msh", content_type); 
     RequestBody request_body = new MultipartBody.Builder() 
       .setType(MultipartBody.FORM) 
       .addFormDataPart("type",content_type) 
       .addFormDataPart("uploaded_file",file_path.substring(file_path.lastIndexOf("/")+1), file_body) 
       .build(); 

     Request request = new Request.Builder() 
       .url("http://fakeurl.com/retrofit_example/save_file.php?neran="+neran) 
       .post(request_body) 
       .build(); 



      response = client.newCall(request).execute(); //exception is here 



     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
      /*uploadFile(selectedFilePath);*/ 
      upsuccess = false; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      upsuccess = false; 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
      /*uploadFile(selectedFilePath);*/ 
      upsuccess = false; 
     } 

     if(response!=null) { 
      if (response.isSuccessful()) { 
       upsuccess = true; 
      } 
      response.body().close(); 
     } 

我已经试过一切,但错误依然存在。虽然上传失败okhttp但其他方法和库也有上述问题。

其他答案还建议:

  1. 增加上传文件大小:我的文件仅仅是55 kb和限制是512 MB
  2. PHP日志不显示上传任何失败

UPDATE:我不完全确定,但这个问题通常发生在WIFI有时不与移动互联网。此外,我注意到有时关闭wifi然后打开似乎解决了问题,但截至目前由于各种原因,我不想在我的应用程序上编程执行此操作,因为我需要额外的权限。任何人都可以告诉我这是什么原因?

回答

0

我之前遇到过这样的问题,当我用okhttp上传一个大文件到服务器时,它发生了异常“sendto failed:ECONNRESET(连接重置由对等)”,它困扰了我好几天,最后我找到了答案。

nginx服务器的配置限制了上传文件的大小。 找到文件“nginx.conf”,修改“client_max_body_size”为512M或更大,然后一切正常。

对不起,我可怜的英语,希望这会帮助你。

相关问题