1.因为是要从网络上获取内容,所以我们首先在清单文件中加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
2.导入依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
3-----------
//创建okhttpclient对象
OkHttpClient okHttpClient = new OkHttpClient();
//创建请求
Request request = new Request.Builder()
//url是要访问的网址
.url(url)
.build();
//创建call对象
Call call = okHttpClient.newCall(request);
try {
//响应
Response execute = call.execute();
ResponseBody body = execute.body();
byte[] bytes = body.bytes();
String data = new String(bytes);
Log.e("123456", data);
return data;
} catch (IOException e) {
e.printStackTrace();
}