Android - setSoTimeout nie działa
Więc pracuję w nie działającym limicie czasu gniazda. Postępowałem zgodnie ze wszystkimi instrukcjami podanymi przez istniejące posty, ale nadal nie działa (nigdy nie otrzymałem wyjątku limitu czasu gniazda). Oto mój kod:
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);
Powinienem uzyskać wyjątek timeout gniazda po dwóch sekundach, ale klient po prostu to ignoruje. Jakaś pomoc?
Z góry dziękuję
Oto cały kod:
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);
}