POST 带参数 封装 外部返回数据
1. 封装
public class OkhttpMethod {
/**
* Post请求 异步
* 使用 Callback 回调可返回子线程中获得的网络数据
* @param url
* @param params 参数
*/
public static void httpPost(final String url, final Map<String,String> params, final Callback callback) {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
FormBody.Builder formBodyBuilder = new FormBody.Builder();
Set<String> keySet = params.keySet();
for(String key:keySet) {
String value = params.get(key);
formBodyBuilder.add(key,value);
}
FormBody formBody = formBodyBuilder.build();
Request request = new Request
.Builder()
.post(formBody)
.url(url)
.build();
//Response response = null;
okHttpClient.newCall(request).enqueue(callback);
}
}).start();
}
}
2. 使用
OkhttpMethod.httpPost(URL, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//异常
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//此处为正常返回进行的操作
responseData = response.body().string(); //提前定义全局变量 String类型
//接下来可对获得的数据进行解析或其他处理
}
});
POST 不返回数据:
public static void httpPost2(final String url, final Map<String,String> params) {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
FormBody.Builder formBodyBuilder = new FormBody.Builder();
Set<String> keySet = params.keySet();
for(String key:keySet) {
String value = params.get(key);
formBodyBuilder.add(key,value);
}
FormBody formBody = formBodyBuilder.build();
Request request = new Request
.Builder()
.post(formBody)
.url(url)
.build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
POST application/json 格式传递
参考:https://www.jianshu.com/p/1f432a7e8b5c
1. 封装
/**
*POST 参数数据类型是application/json
*/
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public final static int CONNECT_TIMEOUT = 60;
public final static int READ_TIMEOUT = 100;
public final static int WRITE_TIMEOUT = 60;
public static final OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取超时时间
.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)//设置写的超时时间
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)//设置连接超时时间
.build();
public static String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.addHeader("token", Constants.token)
.post(body)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
2. 使用
new Thread() {
@Override
public void run() {
try {
String callStr = OkhttpMethod.post("json形式的string");
//解析
AddCartResultVo cartResultVo = JSON.parseObject(callStr, AddCartResultVo.class);
if (cartResultVo.getCode()==0){
// 在子线程中调用ui线程
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ScenicOrderActivity.this, "成功!", Toast.LENGTH_SHORT).show();
finish();
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
如果POST请求同时需要传递query和body,如下图
可将Query请求放在url中,(?key=value&key=value)
GET 不带参数:
1. 封装
public static void httpGet(final String url, final Callback callback) {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).addHeader("token",Constants.token).build(); //添加头部信息
okHttpClient.newCall(request).enqueue(callback);
}
}).start();
}
2. 使用
这样即可把从子线程中获得的数据在主线程进行操作
OkhttpMethod.httpGet(URL, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
responseData = response.body().string(); //得到返回的数据
jsonJXdata(responseData); //解析
}
});
GET 带参数直接加在URL后面即可
如:url="http://xxxxxx" 带上参数变为 url="http://xxxxxxxxx?key=value"
带多个参数即 url="http://xxxxxxxxx?key1=value1&key2=value2"