How to cancel an HTTP request

This is my first stack overflow question. Usually I can find the answers myself, but this time I can’t find a decent answer anywhere. I'm also new to building Android apps, so sorry in advance for my noob'nes.

How to cancel an HTTP request in "android java" after x miliseconds when the local IP address is unavailable? I am requesting an html page using AsyncTask. The class code is given below.

Now I have the following, a timer defined in onPreExecute (), which sets onCancel () to true after X miliseconds. doInBackground () opens a stream, etc., after which it writes the stream to a string.

The problem is that when the local ip address is not available, the url.openStream () function continues to work until java.net.ConnectException is thrown due to timeOut. I do not know how to intercept this command using onCancel () (if possible).

So how do I intercept the url.openStream command? or just terminate the AsyncTask stream as a whole?

private class htmlToString extends AsyncTask<Void,Void,Void> { public htmlToString asyncObj; @Override protected void onPreExecute(){ asyncObj = this; new CountDownTimer(connectionTimeout, connectionTimeout) { public void onTick(long millisUntilFinished) {} public void onFinish() { // stop async task if not in progress if (asyncObj.getStatus() == AsyncTask.Status.RUNNING) { asyncObj.cancel(true); // <- how can i cancel the doInBackground with this ? Log.i("timer", "...still trying to connect..."); } } }.start(); } @Override protected Void doInBackground(Void... params) { try { URL url = new URL(url_string[button_state]); final InputStream url_inputstream = url.openStream(); // <- if the local ip adress is unavailable, this command keeps running until ETIMEDOUT kicks in InputStreamReader url_inputstreamreader = new InputStreamReader( url_inputstream ); BufferedReader in = new BufferedReader( url_inputstreamreader ); String inputLine; while ((inputLine = in.readLine()) != null) page = page.concat(inputLine); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void params){ // some code } } 

Edit1: This is done in Android Studio 1.5.1

Edit2: I solved it myself. I am just reducing the time given by java. I did this in doInBackground () (the code is below). It also meant that I could completely discard the timer inside onPreExecute ().

  @Override protected Void doInBackground(Void... params) { try { URL url = new URL(url_string[button_state]); URLConnection url_connection = url.openConnection(); url_connection.setConnectTimeout(connectionTimeout); // timout is set here final InputStream url_inputstream = url_connection.getInputStream(); InputStreamReader url_inputstreamreader = new InputStreamReader( url_inputstream ); BufferedReader in = new BufferedReader( url_inputstreamreader ); String inputLine; while ((inputLine = in.readLine()) != null) page = page.concat(inputLine); } catch (Exception e) { e.printStackTrace(); } return null; } 
+5
source share
2 answers

httpclient.getConnectionManager().shutdown();

+2
source

you can link below code to cancel the request

  httpget.abort(); 

Example:

 import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class ClientAbortMethod { public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpget = new HttpGet(<URL>); System.out.println("Executing request " + httpget.getURI()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // Call abort on the request object httpget.abort(); } finally { response.close(); } } finally { httpclient.close(); } } } 
+2
source

Source: https://habr.com/ru/post/1242004/


All Articles