如何停止的HttpURLConnection连接在Android [英] How to stop HttpURLConnection connect on Android

查看:134
本文介绍了如何停止的HttpURLConnection连接在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的AsyncTask到URLPath连接如下code。

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
        }
}

连接可能会花很多时间在连接。
虽然它的连接,我想设置Bbutton BTN是pssed停止连接来连接$ P $。
在setOnClickListener方法,如何设置?

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?

推荐答案

您可以尝试停止你的AsyncTask 取消(布尔)。但是,你还需要检查 isCancelled()在你的AsyncTask停止其工作。

You may try to stop your asynctask by cancel(boolean). However, you also need to check isCancelled() in your asynctask to stop its works.

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

    task = new Connecting().execute();

    Button btn = (Button)findViewById(resid);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            task.cancel(true); //or false
        }
    });
}

和您的AsyncTask的处理。

and handle it in your asynctask.

        @Override
        protected String doInBackground(String... aurl) {

            try {

                if(!isCancelled()){
                    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();                       
                }
                else
                {
                    connection.disconnect();
                }               

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

希望这有助于。

Hope this help.

这篇关于如何停止的HttpURLConnection连接在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆