我正在發出HTTP請求。我想要檢查一下,如果我在10秒內沒有得到回覆,我會退出循環。以下是我的代碼。在發出HTTP請求時處理這個問題最簡單的方法是什麼?建議歡迎。如果你能讓我知道我應該在我的代碼中包含這些建議的位置,這將非常有幫助。由於如果響應在Android中的HTTP連接中延遲,則處理
protected String doInBackground(String... params) {
if (STATUS_STARTED != statusCode) {
return null;
}
final HttpClient client = new DefaultHttpClient();
String email = null;
String url = null;
try {
email = params[0];
url = params[1];
} catch (ArrayIndexOutOfBoundsException e) {
Log.e("LOG_TAG", "Invalid parameters are passed to task", e);
statusCode = STATUS_BADPARAM;
return null;
}
if ((null == url) || (null == email)) {
Log.e("LOG_TAG", "One of input params is null: url=" + url + "; email=" + email);
statusCode = STATUS_BADPARAM;
return null;
}
if(email != null){
email = email.trim().replaceAll(" ", "");
//email = email.toLowerCase();
Log.d("EmailLowerCase", "EmailValidator" + email);
}
//url += REQUEST_PARAM_START + REQUEST_PARAM_EMAIL + email;
url += email;
Log.d("LOG_TAG", "Execute activation request to " + url);
HttpGet getRequest = null;
try {
getRequest = new HttpGet(url);
} catch (Exception e) {
Log.e("LOG_TAG", e.getMessage());
}
try {
try {
int timeoutConnection = 10 * 1000;
HttpConnectionParams.setConnectionTimeout(client.getParams(), timeoutConnection);
Log.d("LOG_TAG", "Execute activation request to " + url);
statusCode = STATUS_IN_PROGRESS;
HttpResponse response = client.execute(getRequest);
Log.d("LOG_TAG", "Activation request to " + url
+ " completed");
switch (response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
case HttpStatus.SC_MULTI_STATUS:
case HttpStatus.SC_PARTIAL_CONTENT:
case HttpStatus.SC_RESET_CONTENT:
case HttpStatus.SC_CREATED:
case HttpStatus.SC_ACCEPTED:
case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
statusCode = STATUS_COMPLETED;
break;
case HttpStatus.SC_GATEWAY_TIMEOUT:
statusCode = STATUS_GATEWAY_TIMEOUT;
return "-1";
default:
Log.e("EmailValidator", "Error "
+ response.getStatusLine().getStatusCode()
+ " during activation, url=" + url);
statusCode = STATUS_ERROR;
break;
}
String responseStr = null;
final HttpEntity entityResponse = response.getEntity();
if (null != entityResponse) {
InputStream inputStream = null;
OutputStream outputStream = null;
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
try {
inputStream = entityResponse.getContent();
outputStream = new BufferedOutputStream(dataStream,
IO_BUFFER_SIZE);
copy(inputStream, outputStream);
outputStream.flush();
responseStr = dataStream.toString();
return responseStr;
} finally {
if (null != inputStream) {
inputStream.close();
}
if (null != outputStream) {
outputStream.close();
}
if (null != dataStream) {
dataStream.close();
}
entityResponse.consumeContent();
}
}
}
catch (ConnectTimeoutException e) {
getRequest.abort();
Log.d("ConnectionTimeOut","Connection timout occured");
} catch (IOException e) {
getRequest.abort();
Log.e("LOG_TAG", "I/O error during activation, url=" + url, e);
} catch (IllegalStateException e) {
getRequest.abort();
Log.e("LOG_TAG", "Incorrect URL: " + url);
} catch (Exception e) {
getRequest.abort();
Log.e("LOG_TAG", "Error during activation, url=" + url, e);
}
} catch (Exception e) {
Log.e("LOG_TAG", "Error during activation, url=" + url, e);
} catch (Throwable e) {
Log.e("LOG_TAG", "Error during activation, url=" + url, e);
}
Log.e("LOG_TAG", TRANSMITTING_ERROR);
return null;
}
好吧,我在哪裏得到它的迴應?我的意思是如果我想在超時時間內烤麪包,那麼?我怎麼做 ? – user45678 2014-10-07 09:08:57
查看編輯的代碼 – Sonali8890 2014-10-07 09:14:34
Hi @ Sonali8890 - 查看我編輯的問題。我做了同樣的建議,我已經放了20秒的延遲在我的服務器,10秒後它應該拋出我一個日誌我保持在「ConnectTimeoutException」內,但沒有出現......我的執行方式是否正確? – user45678 2014-10-07 09:25:36