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ş:
Nasıl Android üzerinde internet erişim...
Nasıl AFNetworking ile bir zaman aşımı...
Nasıl'Zamanı s.Java kullanarak bi...
Nasıl python'in soket al yöntemi ...
Kolayca Ayarlamak için bir Dizi Dönüşt...