【问题标题】:"How to fix this post HTTP request using Android Studio?"“如何使用 Android Studio 修复这个 post HTTP 请求?”
【发布时间】:2019-11-12 18:01:08
【问题描述】:

我正在尝试将 HTTP 请求发布到我的 Web 应用程序。但是没有执行任何操作。我哪里错了?

class AsyncT extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... params){
        try{
            String url1 = "http://172.26.1.1/data/omnidrive";
            URL url = new URL(url1);
            HttpURLConnection httpURLConnection = 
            (HttpURLConnection)url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.connect();
            String data = "[0.10,0.00,0.00]";

            DataOutputStream wr = new           
            DataOutputStream(httpURLConnection.getOutputStream());
            wr.writeBytes(data);
            wr.flush();
            wr.close();
        } catch (MalformedURLException e){
            e.printStackTrace();
        } catch(IOException e){
            e.printStackTrace();
        }
        return null;
    }
}

我希望数据会在 Web 应用程序中更新。

这是我点击按钮后显示的内容

错误

W/Settings:设置 device_provisioned 已从 android.provider.Settings.Secure 移至 android.provider.Settings.Global。 V/HiTouch_HiTouchSensor:用户设置完成。 D/NetworkSecurityConfig:未指定网络安全配置,使用平台默认值 I/ViewRootImpl: jank_removeInvalidNode jank list 中的所有节点都超时

【问题讨论】:

标签: java android android-networking


【解决方案1】:
  • 试试这个可能对你有帮助。

  • 首先在 res 文件夹下创建一个文件夹,命名为 xml,然后在该文件夹中创建一个 xml 文件名 network_security_config

  • 然后在该 xml 文件中添加以下代码行。

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">your url</domain>
        </domain-config>
    </network-security-config>
    
  • 然后在 Manifest 文件中的应用程序标记下面的行。

    android:networkSecurityConfig="@xml/network_security_config"
    

【讨论】:

    【解决方案2】:

    使用应在 XML application 标记内声明 android:usesCleartextTraffic="true"。它用于指示应用程序是否打算使用明文网络流量,例如明文 HTTP。 欲了解更多信息,请访问此网站: https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2019-11-19
      • 2012-07-10
      • 1970-01-01
      • 2012-11-14
      • 2016-11-19
      • 2013-02-03
      相关资源
      最近更新 更多