Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

How to stop HttpURLConnection connect on Android

I use AsyncTask to connect an URLPath as below code.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.left_list);

    Button btn = (Button)findViewById(resid);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //how to stop connect
        }
    });
    new Connecting().execute();
}


class Connecting extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
            super.onPreExecute();
        //do something
    }

        @Override
        protected String doInBackground(String... aurl) {
            try {
                URL url = new URL(URLPath);
                connection = (HttpURLConnection)url.openConnection();
                connection.setConnectTimeout(30000);
                connection.setReadTimeout(30000);
                connection.setDoInput(true);
                connection.setUseCaches(false);
                connection.connect();
                is = connection.getInputStream();

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String unused) {
            super.onPostExecute(unused);
        //access InputStream is
        }
}

connection may spend much time in connect.
While it connecting, I want to set the Bbutton btn is pressed to stop the connection connect.
How can I set in setOnClickListener method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Wrap the code that uses HttpURLConnection inside a Future, which you can cancel at will. You can also use the Future's timeout feature, since the timeouts in HttpURLConnection are not reliable.

Define an executor and future within your networking class:

final ExecutorService executor = Executors.newCachedThreadPool(Executors.defaultThreadFactory());
Future<MyResult> future;

Then wrap your networking code inside the Future like this:

future = executor.submit(new Callable<MyResult>() {
    @Override
    public MyResult call() throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        try {
            // .. use the connection ...
        } finally {
            connection.disconnect();
        }
    }
});

MyResult result = future.get(TIMEOUT, TimeUnit.SECONDS);

When you want to cancel, you can make this call from any thread:

future.cancel(true);

If the future times out or is canceled, the connection task may continue to block on a HttpURLConnection method, but your application won't be held back. You should still set appropriate timeouts on the connection to speed up freeing of threads and network sockets.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...