哪个是在Android中实现HTTP请求超时的最佳方法?

时间:2011-03-12 15:05:15

标签: java android apache http

那是我的样本!

private void onGet( String command, int requestType ){
    HttpGet getRequest = new HttpGet("http://" + getHost() + ":" + getPort() + getServerCgiPath() + command);
    Client.sendRequest(getRequest, requestType, getCallBack() );
}

...我已经创建了我的HttpClient

    public class Client {    
public static void sendRequest(final HttpUriRequest     request, 
                                     int                RequestType,
                                     ResponseListener   callback    ) {
    (new AsynchronousSender(new DefaultHttpClient(), request, RequestType, 
                            new Handler(), new CallbackWrapper(callback))).start();
}

}

...

public class AsynchronousSender extends Thread { 
private DefaultHttpClient httpClient; 
private HttpUriRequest    request;
private Handler           handler;
private CallbackWrapper   wrapper;
private int               requestType;
protected AsynchronousSender( DefaultHttpClient httpClient, HttpUriRequest request,
        int requestType, Handler handler, CallbackWrapper wrapper ) {
    setRequest      (request);
    setHandler      (handler);
    setWrapper      (wrapper);
    setClient       (httpClient);
    setRequestType  (requestType);
}

public void run() {
    try {
        final HttpResponse response;
        final HttpParams httpParams = getClient().getParams();  
        HttpConnectionParams.setConnectionTimeout  ( httpParams, 5000 );
        HttpConnectionParams.setSoTimeout          ( httpParams, 5000 );
        Log.d("Ehome", "before execute reauest");
        response = getClient().execute( getRequest() );
        Log.d("Ehome", "post execute reauest");
        int status = response.getStatusLine().getStatusCode();
        switch( status ){
            case HttpStatus.SC_OK:
                getWrapper().setResponse(response);
                getWrapper().setRequestType(getRequestType());
                getHandler().post(getWrapper());
                break;
            default:
                break;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private HttpClient getClient() {
    return httpClient;
}

...

1 个答案:

答案 0 :(得分:1)

HttpConnectionParams是要使用的类:

示例代码(未完成的代码: - ))

HttpClient httpClient = new DefaultHttpClient();
final HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);

所以你必须这样做(没有深入检查你的代码):

    public class Client {    

        public static void sendRequest(final HttpUriRequest request, 
                                       int RequestType,
                                       ResponseListener callback) {
        HttpClient httpClient = new DefaultHttpClient();
        final HttpParams httpParams = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, 5000);
        (new AsynchronousSender(httpClient, request, RequestType, 
                            new Handler(), new CallbackWrapper(callback))).start();

}