10

下面是我的代码:

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(). 但是如果我stopupload()在代码在 line 处理时调用is = connection.getInputStream();,它仍然需要等待它的回复。

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

4

3 回答 3

2

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

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

创建:

@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...
}
于 2013-03-13T10:39:53.763 回答
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) {

        }      
     }   
  }
于 2013-03-13T11:19:19.877 回答
0

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

于 2014-10-21T14:24:34.703 回答