OKhttp的使用注意事项
okhttp简介
OkHttp是一个高效的HTTP库,okhttp对网络通信中的通信协议,数据解析,I/O,缓存,并发请求实现了良好的策略,并拓展和支持了Application/NetWork拦截,HTTP2协议,数据ZIP压缩等功能。
okhttp使用
添加依赖:(个人认为版本高的较好,因为我在使用过程中曾调用某个方法报错,原因就是okhttp版本是3.6.0,改为3.9.0问题解决)
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
通常与Gson配合使用
implementation 'com.google.code.gson:gson:2.8.1'
okhttp请求后端
网上有大量的开源的封装代码提供使用,可降低代码的复杂度,提高可读性,我是参考的这篇文章:(写的贼棒)
https://blog.csdn.net/Lrici/article/details/56679996?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-1&spm=1001.2101.3001.4242
几个注意事项:
okhttp进行POST请求时,表单的提交和raw形式的提交不一样
表单的提交是提交一个JSON,raw形式是个json字符串,也就是raw提交的requestBody是个字符串
raw提交:http://localhost:8880/login
public void sendPostraw(String url, Map<String, String> param, final Fun4 callback) throws JSONException {
JSONObject jsonObject=new JSONObject();
if (param != null && !param.isEmpty()) {
for (Map.Entry<String, String> entry : param.entrySet()) {
jsonObject.put(entry.getKey(), entry.getValue());
}
}
String json = jsonObject.toString();
System.out.println(json);
//JSON:MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody request_body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url(url).post(request_body).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onSuccessJsonObjectMethod(response.body().string(), callback);
}
}
});
}
表单提交:http://localhost:8880/login?username=xxx&password=xxx
public void sendComplexForm(String url, Map<String, String> param, final Fun4 callback) {
FormBody.Builder form_builder = new FormBody.Builder();
if (param != null && !param.isEmpty()) {
for (Map.Entry<String, String> entry : param.entrySet()) {
form_builder.add(entry.getKey(), entry.getValue());
}
}
RequestBody request_body = form_builder.build();
Request request = new Request.Builder().url(url).post(request_body).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onSuccessJsonObjectMethod(response.body().string(), callback);
}
}
});
}
POST提交字符串:类似http://localhost:8880/login/{username}
public void sendStringByPost(String url, final Fun4 callback) {
//MEDIA_TYPE_MARKDOWN:MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown;charset=utf-8");
Request request = new Request.Builder().url(url).post(RequestBody.create(MEDIA_TYPE_MARKDOWN, "")).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response != null && response.isSuccessful()) {
onSuccessJsonObjectMethod(response.body().string(), callback);
}
}
});
}