【问题标题】:How to stop HttpURLConnection.getInputStream()?如何停止 HttpURLConnection.getInputStream()?
【发布时间】:2013-03-01 03:11:12
【问题描述】:

下面是我的代码:

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

当我上传文件时,is = connection.getInputStream(); 行会花费大量时间来获得回复。所以我想实现一个停止功能为stopupload()。但是如果我在代码在is = connection.getInputStream(); 行处理时调用stopupload(),它仍然需要等待它的回复。

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

【问题讨论】:

  • Thread.interrupt() (编辑:实际上,这可能不起作用,因为此 API 不可中断)
  • 有没有不用线程的方法?
  • connection = null; is = null; 是否真的停止了 AsyncTask 的执行?我之所以问是因为connection = null; 对我不起作用,而且我怀疑is = null; 是否会起作用,因为is 仅在getInputStream 的末尾分配。最后,你真的得到了你想要的吗?

标签: java android httpurlconnection


【解决方案1】:

但是如果我在代码在is = connection.getInputStream(); 行处理时调用stopupload(),它仍然需要等待它的回复。

HoneyComb开始,所有的网络操作都不允许在主线程上执行。为避免收到NetworkOnMainThreadException,您可以使用ThreadAsyncTask

我想在执行 stopupload() 时立即停止等待。我怎样才能 做吗?

以下代码让用户在 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...
}

【讨论】:

  • 这个被接受的解决方案到底是怎么回事? stopUpload() 没有实现。
【解决方案2】:
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();

    Thread t = new Thread() {
        public void run() {
             is = connection.getInputStream();                
        }
    };
    t.start()
} catch (Exception e) {
    e.printStackTrace();
}catch (InterruptedException e) {
        stopupload(connection ,is, t);
    }      
}

public void stopupload(HttpURLConnection connection ,InputStream  is,Thread t) {
  if(connection != null ){
    try {
           t.interupt();
               running = false;
               connection=null;
           is=null;
        } catch (Exception e) {

        }      
     }   
  }

【讨论】:

    【解决方案3】:

    将使用HttpURLConnection 的代码包装在Future 中,如here 所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      • 2015-08-14
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      相关资源
      最近更新 更多