Android - setSoTimeout não funciona
Então, eu estou correndo em um tempo limite de soquete não funcionando. Eu segui todas as instruções dadas pelas mensagens existentes, mas ainda não está funcionando (nunca recebo uma exceção de tempo limite de soquete). Aqui está o meu código:
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String location = params[0];
try {
HttpGet httpGet = new HttpGet(location);
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 = 0;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
HttpResponse response = client.execute(httpGet);
Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
return new Scanner(out.toString()).useDelimiter("\\A").next();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
try {
// doStuff
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute(location);
Portanto, devo obter uma exceção de tempo limite de soquete após dois segundos, mas o cliente está simplesmente ignorando isso. Qualquer ajuda?
desde já, obrigado
Então aqui está todo o código:
public void fetch(String location) {
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String location = params[0];
try {
HttpGet httpGet = new HttpGet(location);
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 = 0;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
HttpResponse response = client.execute(httpGet);
Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
return new Scanner(out.toString()).useDelimiter("\\A").next();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
try {
//doStuff
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute(location);
}