W/System:资源无法调用end.当使用HttpsURLConnection时-Android



在logcat中,我收到一条警告:W/系统:资源无法调用end

我百分之百肯定这段代码发出了警告,因为当我把它取出来时,它就停止了。我似乎无法修复它,所以它不会显示警告。

该代码的目的是检查是否有互联网连接。

它在单独的线程上。声明人:

public class ConnectWifiThread extends Thread {
public static boolean isInternetAvailable(Context context) {

这是代码:

try {
URL url = new URL("https://www.google.com/");
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setRequestProperty("User-Agent", "test");
https.setRequestProperty("Connection", "close");
https.setConnectTimeout(2000); // mTimeout is in seconds
https.connect();
int tempResponse = https.getResponseCode();
if (tempResponse == 200) {
https.disconnect();
Thread.sleep(50);
https=null;
url=null;
Thread.sleep(50);
Log.d("Has", "internet");
return true;
} else {
https.disconnect();
Thread.sleep(50);
https=null;
url=null;
Thread.sleep(50);
Log.d("NO", "internet");
return false;
}
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
catch (Exception e) {
Log.d("Error checking internet", e.getMessage());
return false;
}

谢谢

当您使用URLConnection时,默认情况下它会生成一个输入流供您读取响应。您可以通过在连接上调用getInputStream()来获得它,然后读取流以完成并关闭它

如果您不需要这些数据,您也可以调用setDoInput(false)来省去执行上述操作的麻烦。

最新更新