java - HttpURLConnection.getResponse 代码错误

标签 java android http httpurlconnection

在我的 Android 应用程序中,我试图检查是否有互联网访问以及是否有下载一些数据。但是,当我运行下面的代码来验证连接时,.getResponseCode() 方法会返回 -1 而不是 200,即使存在有效连接。设备通过 4G 或 WIFI 连接到互联网,但无法正常工作。设备上安装的其他应用程序可以访问互联网。

protected Boolean doInBackground(String... args) {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            try {
                URL url = new URL("http://www.google.com");
                HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                urlc.setConnectTimeout(3000);
                urlc.connect();
                if (urlc.getResponseCode() == 200) {
                    return true;
                }
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

AndroidManifests.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sg.help">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

更新添加了以下调试输出。正如您所看到的,响应代码是-1。

urlc = {HttpURLConnectionImpl@19895} "com.android.okhttp.internal.http.HttpURLConnectionImpl:http://www.google.com"
 client = {OkHttpClient@19909} 
 handshake = null
 httpEngine = {HttpEngine@19910} 
 httpEngineFailure = null
 requestHeaders = {Headers$Builder@19911} 
 route = {Route@19912} 
 fixedContentLength = -1
 redirectionCount = 0
 method = {String@19913} "GET"
 responseMessage = null
 fixedContentLengthLong = -1
 chunkLength = -1
 fixedContentLength = -1
 instanceFollowRedirects = true
 responseCode = -1
 contentType = null
 defaultHandler = {URLConnection$DefaultContentHandler@19914} 
 url = {URL@19883} "http://www.google.com"
 allowUserInteraction = false
 ifModifiedSince = 0
 lastModified = -1
 connectTimeout = 0
 connected = true
 doInput = true
 doOutput = false
 readTimeout = 0
 useCaches = true
 shadow$_klass_ = {Class@17437} "class com.android.okhttp.internal.http.HttpURLConnectionImpl"
 shadow$_monitor_ = -1761921262

最佳答案

尝试更改您的网址,例如 https://www.android.com/ 。另外,请稍后尝试断开 httpconnection,看看是否有帮助。

Disconnect. Once the response body has been read, the HttpURLConnection should be closed by calling disconnect(). Disconnecting releases the resources held by a connection so they may be closed or reused.

http://developer.android.com/reference/java/net/HttpURLConnection.html

尝试以下操作。

URL url = new URL("https://www.android.com");
                HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                urlc.setConnectTimeout(5000);
                urlc.connect();
                if (urlc.getResponseCode() == 200) {
                    urlc.disconnect();
                    return true;
                }

关于java - HttpURLConnection.getResponse 代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35400907/

相关文章:

c++ - 有没有类似于 apache httpcomponents 的 C/C++ 库?

java - 二进制转换为十进制的输出不是预期的?

java - Java 中的动态 VectorDrawable 动画而不是动画 vector 可绘制对象

Android:应用程序关闭时保存 ListView 数据

java - 对我的 ArrayAdapter 进行排序不会更改我的数据源

java - 映射为 Spring Boot 中查询参数的值

http - 无法下载附件文件 - cURL 工作正常 - 缺少 header ?

java - 检查数组列表中的重复项

java - 为什么有人会在 java 1.8 的接口(interface)中定义静态方法?

循环播放声音时 Android MediaPlayer 警告