第一行代码(第2版):菜鸡踩坑系列----9.2.2使用OkHttp

第一行代码 9.2.2使用OkHttp

哇哇哇 终于 找到这个坑坑 实现了该出现的东西

这一章 是使用网络技术,前面的使用HttpURLConnection 很顺利就实现了书上的东西。
但是在进入使用OkHttp后,按照书上敲完代码,运行时点击请求按钮,没有了任何反应。
查看了OkHttp 需要依赖okio这个东西,添加后还是没有什么效果、
都是最新的版本:OkHttp的github地址:https://github.com/square/okhttp
Okio的github地址:https://github.com/square/okio
我的配置文件

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation("com.squareup.okhttp3:okhttp:4.2.2")
    implementation("com.squareup.okio:okio:2.4.2")
}

在尝试后并没得到自己想要的结果,隔了几天,就在刚刚 查看日志后,发现了这么一个错误

W/System.err: java.net.UnknownServiceException: CLEARTEXT communication to www.baidu.com not permitted by network security policy

顺着这个错误,通过查找得到了我想要的结果了。
先放链接:https://blog.csdn.net/qq_2300688967/article/details/81114201
添加后就可以了。
原理我不清楚,我也才开始学Android。
我的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.networktest">
    <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:theme="@style/AppTheme"
        ------------------------------------
        android:usesCleartextTraffic="true">
        ----------------------------------------
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值