SORU
29 Mart 2009, Pazar


Nasıl HttpResponse zaman aşımı ayarlamak için Java Android için

Bağlantı durumunu kontrol etmek için: aşağıdaki fonksiyonu oluşturdum

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

Aşağı test için sunucu kapattığımda yürütme çizgisinde uzun bir zaman bekler

HttpResponse response = httpClient.execute(method);

Herkes çok uzun süre beklemesini önlemek için zaman aşımı süresini ayarlamak için nasıl biliyor mu?

Teşekkürler!

CEVAP
14 EKİM 2009, ÇARŞAMBA


Benim örnekte iki zaman aşımı ayarlanır. Bağlantı zaman aşımı atar "java.net.SocketTimeoutException: Yuva" ve soket zaman aşımı "java takılmadı.net.SocketTimeoutException: işlem zaman aşımına uğradı".

HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);

Eğer herhangi bir mevcut HTTPClient Parametreleri (örneğin DefaultHttpClient veya AndroidHttpClient) ayarlamak istiyorsanız bu fonksiyonu kullanabilirsiniz() setParams.

httpClient.setParams(httpParameters);

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • 2ndfloor91

    2ndfloor91

    17 Kasım 2007
  • 8bitdigitaltv

    8bitdigitalt

    31 AĞUSTOS 2011
  • MarinaHD2001

    MarinaHD2001

    7 ŞUBAT 2009