下面是我的代码:
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()
。我该怎么做?