异常随机发生,然后很难摆脱。我厌倦了这个错误,尽管事实上我已经尝试了几乎所有的stackoverflow,从更改网址到相应的IP地址,使用真实设备,添加互联网权限,或检查wifi连接。一切都没事,除了不断回来的错误。是的我还添加了检查连接代码,如下所示:
try {
ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
URL url = new URL(urlx);
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setConnectTimeout(1000); // mTimeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
runOnUiThread(new Runnable() {
public void run() {
//Do something on UiThread
upsuccess = true;
}
});
} else {
runOnUiThread(new Runnable() {
public void run() {
//Do something on UiThread
upsuccess = false;
}
});
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
和System.setProperty("http.proxyHost", "myurl.com");
System.setProperty("http.proxyPort", "8080");
但是它们中的每一个单独以及一起都没有取得任何有用的结果。你能建议我做什么吗?
我基本上是这样做的:我正在我的服务器上传一个pdf文件,并用该网址加载google docs viewer url。但是,我多次看到以下异常。我还添加了重试,以防我发现异常,但异常随机发生,如果遇到它就不容易退出。
例外:
java.net.UnknownHostException:无法解析主机" myurl.com":否 与主机名相关的地址07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:95) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在 java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在java.net.InetAddress.getAllByName(InetAddress.java:752)07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at okhttp3.Dns $ 1.lookup(Dns.java:39)07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at okhttp3.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:173) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在 okhttp3.internal.http.RouteSelector.nextProxy(RouteSelector.java:139) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在okhttp3.internal.http.RouteSelector.next(RouteSelector.java:81) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在 okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:172) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在 okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在 okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在okhttp3.RealCall.getResponse(RealCall.java:243)07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at okhttp3.RealCall $ ApplicationInterceptorChain.proceed(RealCall.java:201) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在okhttp3.RealCall.execute(RealCall.java:57)07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at securitymsg.listmydocs.CloudViewer $ UploadFileAsync.doInBackground(CloudViewer.java:678) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在 securitymsg.listmydocs.CloudViewer $ UploadFileAsync.doInBackground(CloudViewer.java:650) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在android.os.AsyncTask $ 2.call(AsyncTask.java:305)07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237)07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243)07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:607) 07-20 01:12:11.554 24907-24997 / securitymsg.listmydocs W / System.err:
在java.lang.Thread.run(Thread.java:761)07-20 01:12:13.016 24907-24907 / securitymsg.listmydocs W / cr_BindingManager:无法调用 determineVisibility() - 从未看到pid的连接:24907 07-20 01:12:13.677 24907-24907 / securitymsg.listmydocs W / cr_BindingManager:无法调用determineVisibility() - 从未见过 连接pid:24907 07-20 01:12:13.678 24907-24907 / securitymsg.listmydocs W / cr_BindingManager:无法调用 determineVisibility() - 从未看到pid的连接:24907
我在舱单中的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我上传文件的代码:
* PHP:
<?php
$file_path = "images/";
$neran = $_GET['neran'];
$ext = pathinfo(basename( $_FILES['uploaded_file']['name']), PATHINFO_EXTENSION);
$file_path = "images/".$neran.".".$ext;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path) ){
echo "success";
} else{
echo "fail";
}
&GT;
Android方面(我使用Retrofit):以下代码在AsyncTask的doinbackground块中
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://reviewitapp.co/retrofit_example/save_file.php?neran="+neran)
.post(request_body)
.build();
response = client.newCall(request).execute();
} 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();
}