ok依赖
implementation 'com.squareup.okhttp3:okhttp:3.6.0'
implementation 'com.squareup.okio:okio:1.11.0'
网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
简单的网络请求
new Thread(
new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.baidu.com")
.build();
final Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
Log.i("xxxx",string);
}
});
}
}
).start();
https://www.jianshu.com/p/d2d294fa4d48