Android 调用 API 报错问题 No Network Security Config specified, using platform default

报错

No Network Security Config specified, using platform default

问题由来(原因)

大概是因为Android P(9.0)后,访问API接口需要加密…

解决办法

创建一个 network_security_config.xml,用来声明证书校验方式
文件目录
network_security_config.xml 代码内容:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config> //默认配置:允许明文通信
    <base-config cleartextTrafficPermitted="true" /> 
</network-security-config>

在AndroidManifest.xml 引入network_security_config.xml,注意在AndroidManifest.xml中添加一行android:usesCleartextTraffic=“true”

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">

如果按照上述修改后还有报错

D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
W/System.err: java.io.IOException: Cleartext HTTP traffic to XXX.XXX.XXX.XXX not permitted

解决方法

使用OkHttp请求API,这个问题困扰了我一天,发现换了一个方法就解决了,之前用的是HttpURLConnection,请求一直报错
OkHttp POST方法:

//okHttp POST
    public static String okhttpPost(String url, String json) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

OkHttp GET方法:

//okHttp Get
    public static void okhttpGet(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        Response response = call.execute();
        String content = response.body().string();
        System.out.println(content);
    }

这里我调用POST方法,需要新建一个线程来调用,否则会报错,示例如下:

public void Check(View v) {
        new Thread() {
            @Override
            public void run() {
                GetApi getApi = new GetApi();
                String url = getResources().getString(R.string.ipPort) +"/api/kgas/LoginUser";
                JSONObject jsonDate = new JSONObject();
                jsonDate.put("userName",userName.getText().toString());
                jsonDate.put("passWord",Password.getText().toString());
                try {
                    String callStr = getApi.okhttpPost(url, jsonDate.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

参考地址:Android OkHttp完全解析 是时候来了解OkHttp了

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值