下面是我的代码:如何停止HttpURLConnection.getInputStream()?
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不可中断) – njzk2
是否有任何方法没有使用线程? – brian
做'connection = null; is = null;'实际上停止了AsyncTask的执行?我在问,因为'connection = null;'不适用于我,我怀疑is = null;'只会在getInputStream结束时才分配is。你最终得到了你想要的东西吗? – doplumi