OkHttp3 简单使用
- AS 项目 build.gradle 中添加:
compile 'com.squareup.okhttp3:okhttp:3.9.0'
- AndroidManifest.xml 添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
- Okhttp3
get
代码请求
//创建 OkHttp 对象
private final OkHttpClient client = new OkHttpClient();
//创建 Request 对象,通过内部类 Builder 调用生成 Request 对象
Request request = new Request.Builder()
.url("https://www.sogou.com/")
.build();
//创建 Call 对象,调用 execute(同步请求)/enquene(异步请求)
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: ");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "onResponse: "+response.body().string());
}
});
- Okhttp3
post
代码请求,( //todo)
Okhttp3 GitHub 官方链接:https://github.com/square/okhttp