加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何停止HttpURLConnection.getInputStream()?

发布时间:2020-12-14 05:03:02 所属栏目:Java 来源:网络整理
导读:以下是我的代码: private HttpURLConnection connection;private InputStream is;public void upload() { try { URL url = new URL(URLPath); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(30000); connection.se
以下是我的代码:
private HttpURLConnection connection;
private InputStream is;

public void upload() {
    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();
    }
}

public void stopupload() {
    connection = null;
    is = null;
}

当我上传文件时,行为= connection.getInputStream();会花很多时间来回复.所以我想实现stopupload()的停止函数.但是如果在代码在线is = connection.getInputStream())处调用stopupload();它仍然需要等待它的回复.

我想在stopupload()实现时立即停止等待.我该怎么做?

解决方法

But if I call stopupload() while the code is handling at line is =
connection.getInputStream();
,it still needs to wait for its reply.

从HoneyComb开始,所有网络操作都不允许在主线程上执行.为避免获得NetworkOnMainThreadException,您可以使用Thread或AsyncTask.

I want to stop waiting at once while implement stopupload(). How can I
do it?

以下代码让用户在2秒钟后停止上传,但您可以相应地修改睡眠时间(应少于5秒).

上传:

public void upload() {
    try {
        URL url = new URL(URLPath);
        connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.connect();
        // run uploading activity within a Thread
        Thread t = new Thread() {
            public void run() {
                is = connection.getInputStream();
                if (is == null) {
                    throw new RuntimeException("stream is null");
                }
                // sleep 2 seconds before "stop uploading" button appears
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        mBtnStop.setVisibility(View.VISIBLE);
                    }
                },2000);
            }
        };
        t.start();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
}

的onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    // more codes...
    Handler mHandler = new Handler();
    mBtnStop = (Button) findViewById(R.id.btn_stop);
    mBtnStop.setBackgroundResource(R.drawable.stop_upload);
    mBtnStop.setOnClickListener(mHandlerStop);
    mBtnStop.setVisibility(View.INVISIBLE);

    View.OnClickListener mHandlerStop = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopUpload(); // called when "stop upload" button is clicked
        }
    };

    // more codes...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读