我创建了以下用于检查连接状态的函数:
private void checkConnectionStatus() {
HttpClient httpClient = new DefaultHttpClient();
try {
String url = "http://xxx.xxx.xxx.xxx:8000/GaitLink/"
+ strSessionString + "/ConnectionStatus";
Log.d("phobos", "performing get " + url);
HttpGet method = new HttpGet(new URI(url));
HttpResponse response = httpClient.execute(method);
if (response != null) {
String result = getResponse(response.getEntity());
...
Run Code Online (Sandbox Code Playgroud)
当我关闭服务器进行测试时,执行会等待很长时间
HttpResponse response = httpClient.execute(method);
Run Code Online (Sandbox Code Playgroud)
有谁知道如何设置超时以避免等待太久?
谢谢!
我正在从事与服务相关的应用程序.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000000);
HttpConnectionParams.setSoTimeout(params, 15000000);
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(),true);
String str_Server_Host = Control_PWD_Server_Name();
String requestEnvelope = String.format(envelope_oe);
// HttpPost httpPost = new HttpPost(str_Server_Host);
HttpPost httpPost = new HttpPost(str_Server_Host);
httpPost.setHeader("SOAPAction",
"http://tempuri.org/updateTransportStatus");
httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");
httpPost.setEntity(new StringEntity(requestEnvelope));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
String str = Html.fromHtml(line).toString();
Run Code Online (Sandbox Code Playgroud)
这是我得到的例外:
org.apache.http.conn.ConnectTimeoutException: Connect to /10.64.0.184:9092 timed out
Run Code Online (Sandbox Code Playgroud)
如何解决android中的上述异常?请提供任何建议或相关的senario.
我正在从android应用程序向cgi php服务器发出HTTP请求以获取json响应.我的应用程序在发出HTTP请求之前验证Internet连接是否可用.所有事情都正常运作.问题是我因为在发出HTTP请求后突然死于互联网连接而逐渐关闭.
所以我的问题是
android ×3
java ×2
exception ×1
forceclose ×1
http ×1
httpresponse ×1
timeout ×1
web-services ×1