尝试只使用几秒钟的Internet连接

Ven*_*dor 2 java concurrency multithreading android

我正在尝试连接到互联网,我必须获取数据,如果时间超过5秒连接我必须完成该过程并继续脱机工作.
一切都运行正常,有时需要大约10秒才能return上网,现在我必须返回xml == null;当时间超过时间限制时,
我不想在异步任务中执行此操作

    public String getUrlData(String url) {
    String xml = null;

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    System.out.println("waiting");
    HttpResponse httpResponse;

    try {
        // start the timer here

        httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

        // check if the timer has exceeded by "if else"
        // move to "return xml;" Manually when exceeds 5sec, but how?

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return xml;

}
Run Code Online (Sandbox Code Playgroud)

此答案后编辑的代码

public String getUrlData(String url) {
    String xml = null;

    final int TIMEOUT_MILLISEC = 5000; // 5 seconds

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
    HttpPost httpPost = new HttpPost(url);
    System.out.println("waiting");
    HttpResponse httpResponse;

    try {
        // start the timer here
        System.out.println("Started");
        httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Ended");
    return xml;

}
Run Code Online (Sandbox Code Playgroud)

LogCat在这里>> 20秒

waq*_*lam 8

您需要做的就是为连接定义超时限制.例如:

final int TIMEOUT_MILLISEC = 5000;  // 5 seconds

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

HttpClient httpClient = new DefaultHttpClient(httpParams);
Run Code Online (Sandbox Code Playgroud)

然后,httpClient以与使用它相同的方式使用.


编辑

public String getUrlData(String url) {
String xml = null;

final int TIMEOUT_MILLISEC = 5000;  // 5 seconds

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(url);
System.out.println("waiting");
HttpResponse httpResponse;

try {
    // start the timer here

    httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    xml = EntityUtils.toString(httpEntity);

    // check if the timer has exceeded by "if else"
    // move to "return xml;" Manually when exceeds 5sec, but how?

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return xml;

}
Run Code Online (Sandbox Code Playgroud)