第一行代码 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>