SORU
19 HAZİRAN 2010, CUMARTESİ


Http bağlantısı Android çalışmıyor zaman aşımı

Bir Java eklentisi bağlanan bir uygulama yazıyorum ve eğer bir bağlantı olabilir diye çok fazla beklemek istemiyorum. Bu yüzden httpparams bu connectionTimeout ayarlayın. Ama herhangi bir etkisi yok gibi görünmüyor.

Ben test etmek benim WLAN geçici olarak kapatın. Uygulama oldukça uzun bir süre (yol istiyorum 3 saniyeden daha fazla) için bağlanmak için çalışır ve daha sonra bir UnknownHostException atar.

İşte benim kod:

try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 3000);
    HttpConnectionParams.setSoTimeout(params, 3000);

    httppost = new HttpPost(URL);
    StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
    httppost.setEntity(se);
    //Code stops here until UnknownHostException is thrown.
    BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);

    HttpEntity entity = httpResponse.getEntity();
    return entity;

}catch (Exception e){
    e.printStackTrace();
}

Herkesi çok özledim, ne herhangi bir fikir var mı?

CEVAP
19 HAZİRAN 2010, CUMARTESİ


Bu şekilde yapmaya çalışın:

HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
httpPost.setEntity(se);

HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
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 = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
BasicHttpResponse httpResponse = (BasicHttpResponse)  httpClient.execute(httpPost);

HttpEntity entity = httpResponse.getEntity();
return entity;

Daha sonra ConnectTimeoutException yakalamak mümkün olabilir.

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • InfoPuppet

    InfoPuppet

    15 Kasım 2011
  • KendrickLamarVEVO

    KendrickLama

    9 ŞUBAT 2011
  • thenewboston

    thenewboston

    4 ŞUBAT 2008