首页 > 解决方案 > 类 HttpURLConnection 在 Android Studio 中不起作用

问题描述

我是 Android Studio 的新手,发现 HTTP 连接存在一些问题。目标是获取响应包含在我可以解析的 JSON 对象中的服务器。

我在 Netbeans 的应用程序中测试此代码。工作正常。

    try {
        URL url = new URL("http://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        System.out.println("Status " + connection.getResponseCode());
        System.out.println(connection.getHeaderFieldKey(1) + " " + connection.getHeaderField(1));
        System.out.println(connection.getHeaderFieldKey(2) + " " + connection.getHeaderField(2));
        System.out.println(connection.getHeaderFieldKey(3) + " " + connection.getHeaderField(3));
        System.out.println(connection.getHeaderFieldKey(4) + " " + connection.getHeaderField(4));
        System.out.println(connection.getHeaderFieldKey(5) + " " + connection.getHeaderField(5));
        InputStream istream = connection.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(istream));
        StringBuilder response = new StringBuilder();
        String currentLine;            
        while ((currentLine = in.readLine()) != null) 
            response.append(currentLine);
        System.out.print(response.toString());
        connection.disconnect();
    }
    catch (Exception e) {
        System.out.println(e.getMessage());
    }
    finally {}

这里是输出。

 Status 200
 Date Fri, 20 Aug 2021 02:05:02 GMT
 Expires -1
 Cache-Control private, max-age=0
 Content-Type text/html; charset=ISO-8859-1
 P3P CP="This is not a P3P policy! See g.co/p3phelp for more info."
 <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang ....

我在 Android Studio 中尝试了同样的方法,但我现在在 Android 中尝试的却没有。

  URL url = new URL("http://google.com");
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  Log.d("Step", "Before");
  connection.getHeaderFieldKey(1);
  Log.d("Step", "After");

日志猫

 2021-08-19 22:56:51.733 9063-9063/com.mars.cellulose D/MyApp: I am here, onResume
 2021-08-19 22:56:51.736 9063-9063/com.mars.cellulose D/NetworkSecurityConfig: No         Network Security Config specified, using platfo rm default
 2021-08-19 22:56:51.736 9063-9063/com.mars.cellulose D/Step: Before
 2021-08-19 22:56:51.741 9063-9063/com.mars.cellulose D/Error Post: println needs a message
 2021-08-19 22:56:51.767 9063-9087/com.mars.cellulose D/OpenGLRenderer: HWUI GL     Pipeline
 2021-08-19 22:56:51.833 9063-9087/com.mars.cellulose I/OpenGLRenderer: Initialized EGL, ve

它包含在一个通用的 Try Catch 块中,可防止 VM 停止并传递“D/Error Post:println 需要一条消息”。对 connection.getHeaderFieldKey(1) 的唯一方法调用会产生故障。关于空指针的一些事情。

只是补充。这就是我改变清单的方式和它的样子。

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mars.cellulose">
   <uses-permission android:name="android.permission.INTERNET" />
   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:usesCleartextTraffic="true"

作为记录。这是从一个活动的 onResume() 调用的,我真的不知道还能尝试什么。任何帮助,将不胜感激。

标签: javaandroidandroid-studiohttpurlconnection

解决方案


推荐阅读