【发布时间】:2017-12-19 16:37:09
【问题描述】:
我正在使用 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 agqt@1598588 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();
}
我已经尝试了有关 stackoverflow 的其他问题中讨论的所有内容,但错误仍然存在。尽管使用 okhttp 上传失败,但其他方法和库也存在上述问题。
还建议其他答案:
- 增加上传文件大小:我的文件只有 55 kb,限制为 512 MB
- PHP 日志未显示任何内容,上传失败
更新:我不完全确定,但这个问题通常发生在 WIFI 上,有时不会出现在移动互联网上。我也注意到有时关闭 wifi 然后打开似乎可以解决问题,但由于各种原因,我不想在我的应用程序上以编程方式执行此操作,因为我需要额外的权限。谁能告诉我这个问题的原因是什么?
【问题讨论】: