wea*_*ire 28 java android httpclient
当我连接到我的网络服务以检索数据时,手机有时会断开连接,DNS混乱,等等.然后我得到一个UnknownHostException非常好的.
我想要做的是在这里寻找hostName时设置超时:
response = httpclient.execute(httpget);
Run Code Online (Sandbox Code Playgroud)
我已经设定:
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
Run Code Online (Sandbox Code Playgroud)
但他们似乎并不申请HostLookUp.有没有办法在主机查找时设置超时?
编辑
我刚刚发现用户无法修改nslookup hc-dev邮件列表中此帖子的超时.
我将不得不在此时从计时器手动抛出超时异常.
Art*_*iyk -1
你应该使用这样的东西:
/**
* Check availability of web service
*
* @param host Address of host
* @param seconds Timeout in seconds
* @return Availability of host
*/
public static boolean checkIfURLExists(String host, int seconds)
{
HttpURLConnection httpUrlConn;
try
{
httpUrlConn = (HttpURLConnection) new URL(host).openConnection();
// Set timeouts in milliseconds
httpUrlConn.setConnectTimeout(seconds * 1000);
httpUrlConn.setReadTimeout(seconds * 1000);
// Print HTTP status code/message for your information.
System.out.println("Response Code: " + httpUrlConn.getResponseCode());
System.out.println("Response Message: "
+ httpUrlConn.getResponseMessage());
return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3390 次 |
| 最近记录: |