Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查android apache连接的连接超时_Android - Fatal编程技术网

如何检查android apache连接的连接超时

如何检查android apache连接的连接超时,android,Android,我正在做什么::我正在一个异步任务中使用此代码检查internet连接,并且超时。如果连接可用,则返回true;如果连接不可用,则返回false public static boolean hasActiveInternetConnection() { try { HttpClient Client = new DefaultHttpClient(); HttpURLConnection urlc = (

我正在做什么::我正在一个异步任务中使用此代码检查internet连接,并且超时。如果连接可用,则返回true;如果连接不可用,则返回false

public static boolean hasActiveInternetConnection() {
            try {

                HttpClient Client = new DefaultHttpClient();

                HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
                urlc.setRequestProperty("User-Agent", "Test");
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(500); 
                urlc.connect();
                return true;
            } catch (IOException e) {
                Log.e("Log", "Error checking internet connection", e);
            }
        return false;
    }
如何在下面对apache Http客户端执行相同操作:

请尝试一下:

HttpGet httpget=null;
String mContent=null;
HttpClient Client=null;

Client = new DefaultHttpClient();
Client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 500);
httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
    mContent = Client.execute(httpget, responseHandler);
    return true;
} catch (IOException e) {
    e.printStackTrace();
}
return false;

难道你不能像在第一段代码中那样尝试执行吗?@user3249477。。。。。。是的,我可以,但我不知道如何给出超时时间……:
HttpGet httpget=null;
String mContent=null;
HttpClient Client=null;

Client = new DefaultHttpClient();
Client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 500);
httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
    mContent = Client.execute(httpget, responseHandler);
    return true;
} catch (IOException e) {
    e.printStackTrace();
}
return false;
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 500);

    Client = new DefaultHttpClient(params);