如何停止的HttpURLConnection连接在AndroidHttpURLConnection、Android

2023-09-12 10:04:08 作者:笑你狗

我使用的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.

 
精彩推荐
图片推荐