2

データを取得する必要があるインターネットに接続しようとしています。接続に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;

}

この回答の後に編集されたコード

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;

}

ここにLogCat>> 20秒

4

5 に答える 5

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);

その後、それを使用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;

}
于 2012-09-10T08:10:16.460 に答える
2

これはどう:

Thread thread = new Thread() {
  @Override
  public void run() {
    // do the downloading thing..
  }
};
thread.start();
thread.join(5000);
于 2012-09-10T08:10:43.180 に答える
1

これは単なるアイデアですが、遅延実行可能ファイルを設定し、5秒後にファイルにサイズがあるかどうかを確認できます。

于 2012-09-10T08:09:24.710 に答える
0
 HttpParams params = new BasicHttpParams();

    // Turn off stale checking.  Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);

    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    // Don't handle redirects -- return them to the caller.  Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    HttpClientParams.setRedirecting(params, false);

    // Use a session cache for SSL sockets
    SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);

    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http",
            PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https",
            SSLCertificateSocketFactory.getHttpSocketFactory(
            SOCKET_OPERATION_TIMEOUT, sessionCache), 443));

    ClientConnectionManager manager =
            new ThreadSafeClientConnManager(params, schemeRegistry);

    // We use a factory method to modify superclass initialization
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);

必要に応じて、SOCKET_OPERATION_TIMEOUTの値を変更します。

于 2012-09-10T08:11:19.073 に答える
0

このコードはあなたを助けるかもしれません

最後の答えからのこのコードはOKです

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);

リクエストを実行しようとしている場合にのみ、さらに3つの例外SocketTimeoutException、UnknownHostException、ConnectTimeoutExceptionを気にする必要があります。

したがって、この3つの例外もキャッチします。

于 2012-09-12T13:51:34.233 に答える