Hello, I have a class to read an HTML page, but I wanted it to have a time limit for execution, my code seems to do this, but it does not work as it should, that is, Nothing happening
private class GrabURL extends AsyncTask<String, Void, Void> {
InputStream is = null;
int response = -1;
URL url;
URLConnection conn;
String retorno = null;
private ProgressDialog Dialog = new ProgressDialog(TelaLogin.this);
protected void onPreExecute() {
Dialog.setMessage("Carregando. Aguarde...");
Dialog.setCanceledOnTouchOutside(false);
Dialog.show();
}
protected Void doInBackground(String... urls) {
try {
url = new URL(urls[0]);
conn = url.openConnection();
if(!(conn instanceof HttpURLConnection))
{
retorno = "Erro-Tempo";
return null;
}
else
{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setReadTimeout(8000); // 8 segundos para ler
httpConn.setConnectTimeout(4000); // 4 segundos para conectar
httpConn.connect();
response = httpConn.getResponseCode();
if(response == HttpURLConnection.HTTP_OK)
{
is = httpConn.getInputStream();
retorno = inputStreamToString(is).toString();
}
else
retorno = "Erro-Tempo";
}
}catch(SocketTimeoutException ex){
retorno = "Erro-Tempo";
cancel(true);
}catch (ClientProtocolException e) {
retorno = "Invalido-Erro";
cancel(true);
} catch (IOException e) {
retorno = "Invalido-Erro";
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
trataRetorno(retorno);
}
}