如何停止HttpURLConnection.getInputStream()? [英] How to stop HttpURLConnection.getInputStream()?

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

问题描述

下面是我的code:

 私人HttpURLConnection的连接;
私人InputStream为;公共无效上传(){
    尝试{
        网址URL =新的URL(URLPath);
        连接=(HttpURLConnection类)url.openConnection();
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);
        connection.setDoInput(真);
        connection.setUseCaches(假);
        connection.connect();
        是= connection.getInputStream();
    }赶上(例外五){
        e.printStackTrace();
    }
}公共无效stopupload(){
    连接= NULL;
    是=无效;
}

当我上传的文件,该行是= connection.getInputStream(); 会花很多的时间去回复。所以我想实现一个停止功能为 stopupload()。但是,如果我叫 stopupload(),而code是在处理行为= connection.getInputStream(); ,它仍然需要等待答复。

我要立即停止执行,同时等待 stopupload()。我该怎么办呢?


解决方案

  

但是,如果我称之为 stopupload(),而code是在处理行 =是
  connection.getInputStream();
,它仍然需要等待答复


从开始的蜂窝的,所有的网络操作都不允许在主线程执行。为了避免让<一href=\"http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html\">NetworkOnMainThreadException,您可以使用的AsyncTask


  

我要停止等待一次,而执行stopupload()。我怎么能够
  办呢?


下面code使用户能够停止在2秒后上传,但可以修改睡眠时间(应小于5秒)相应。

上传

 公共无效上传(){
    尝试{
        网址URL =新的URL(URLPath);
        连接=(HttpURLConnection类)url.openConnection();
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);
        connection.setDoInput(真);
        connection.setUseCaches(假);
        connection.connect();
        //一个线程中运行上传活动
        线程t =新的Thread(){
            公共无效的run(){
                是= connection.getInputStream();
                如果(是== NULL){
                    抛出新的RuntimeException(流为空);
                }
                //睡眠前2秒停止上传按钮出现
                mHandler.postDelayed(新的Runnable(){
                    公共无效的run(){
                        mBtnStop.setVisibility(View.VISIBLE);
                    }
                },2000);
            }
        };
        t.start();
    }赶上(例外五){
        e.printStackTrace();
    } {最后
        如果(是!= NULL){
            尝试{
                is.close();
            }赶上(IOException异常五){
            }
        }
        如果(连接!= NULL){
            connection.disconnect();
        }
    }
}

的onCreate:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    //更多$ C $ ... CS
    处理器mHandler =新的处理程序();
    mBtnStop =(按钮)findViewById(R.id.btn_stop);
    mBtnStop.setBackgroundResource(R.drawable.stop_upload);
    mBtnStop.setOnClickListener(mHandlerStop);
    mBtnStop.setVisibility(View.INVISIBLE);    View.OnClickListener mHandlerStop =新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            stopUpload(); //调用时停止上传按钮被点击
        }
    };    //更多$ C $ ... CS
}

Below is my code:

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

When I upload file, the line is = connection.getInputStream(); will spend a lot of time to get reply. So I want to implement a stop function as stopupload(). But if I call stopupload() while the code is handling at line is = connection.getInputStream();, it still needs to wait for its reply.

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

解决方案

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

Starting from HoneyComb, all network operations are not allowed to be executed over main thread. To avoid getting NetworkOnMainThreadException, you may use Thread or AsyncTask.

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

Below code gives the user to stop uploading after 2 seconds, but you can modify the sleep times (should be less than 5 seconds) accordingly.

upload:

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

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

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