目录:
这是一个基于OkHttp网络请求的公共类,可以重复使用,提高效率。
OkHttpUtil.java
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class OkHttpUtil {
public static void sendHttpRequest(String address, Callback callback) {
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder().url(address).build();
client.newCall(request).enqueue(callback);
}
}
解析:
使用方法如下:
OkHttpUtil.sendHttpRequest("https://www.baidu.com", new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
String data = response.body().string();
}
@Override
public void onFailure(Call call, IOException e) {
}
});
解析: