okhttp3.0的工具类
来源:互联网 发布:安悦溪演技知乎 编辑:程序博客网 时间:2024/06/06 13:38
异步post请求
1.1//异步请求post 提交 键值对数据(key_value数据)
public void postAyncHttp(String url,Map<String,String> data) {
/* RequestBody formBody = new FormBody.Builder().add("userName", "10")
.add("loginPwd","2323")
.build();*/
for (Map.Entry<String,String> item :data.entrySet()){
builder.add(item.getKey(),item.getValue());
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("msg",e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Log.i("wangshu", str);
new Thread(new Runnable() {
@Override
public void run() {
Toast.makeText(MyApp.getContext(), "请求成功", Toast.LENGTH_SHORT).show();
}
});
}
});
}
1.2异步post请求 提交 json数据
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
private OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build();
public String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(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 {
}
});
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(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 {
}
});
同步post请求
2..1//同步post请求 (参数)提交json数据(因为是同步请求时放在子线程里)
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
private OkHttpClient client = new OkHttpClient.Builder()
// .sslSocketFactory(createSSLSocketFactory())
// .hostnameVerifier(new TrustAllHostnameVerifier())
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build();
public String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
return "";
}
}
例如这样调用上面的2.1方法:= MediaType.parse("application/json; charset=utf-8");
private OkHttpClient client = new OkHttpClient.Builder()
// .sslSocketFactory(createSSLSocketFactory())
// .hostnameVerifier(new TrustAllHostnameVerifier())
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build();
public String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
return "";
}
}
JSONObject req = new JSONObject();req.put("pageIndex", page);req.put("pageSize", pagesize);String response = OKhttpHelper.getInstance().post(GET_LIVELIST, req.toString());
1 0
- okhttp3.0的工具类
- Okhttp3的工具类方法
- 基于okhttp3的工具类HttpUtils
- OkHttp3.0网络请求工具类
- 网络请求之OKHttp3.0(工具类)
- okhttp3 工具类
- Okhttp3封装工具类
- okhttp3的工具类(GsonArrayCallback GsonObjectCallback NetWorkUtils OkHttp3Utils)
- OkHttp3.0的基本使用
- OKhttp3.0的基本使用
- okhttp3.0的简单使用
- okhttp3.0
- okhttp3.0
- okhttp3简单封装GET和POST请求工具类
- OKHttp3.0的日常及入门
- Retrofit2.0 + OkHttp3 遇到的坑
- Android基于Okhttp3的文件下载工具类
- 网络请求工具类之OkHttp3封装(二)上(支持请求取消、异步请求的线程切换)
- SQL 语句按月份统计查询
- POJ 2406 - Power String
- 基于Html5的移动端APP开发框架
- centos中编译安装nginx并支持ssl
- Remove Duplicates from Sorted Array II
- okhttp3.0的工具类
- elasticsearch修改配置文件后启动提示nested: ParserException[expected '<document start>', but found BlockMappingS
- Oracle 11g sys,system 密码忘记设置解决办法
- 阿里云RDS的mysql数据库占用空间超过90%的处理
- 画虚线
- Mac idea还原默认配置
- 蘑菇街电商交易平台服务架构及改造优化历程
- Android Splash界面解决白屏、黑屏等问题
- 在ListView中嵌套ListView的事件处理