使用插件:okhttp-3.10.0和okio-1.14.0
//TOKEN
public static String TOKEN = "";
//请求端口号
public final static String address = "http://127.0.0.1:7001";
//转换Map类型为JSON对象类型
public static String JSONStringify(Map<String,Object> body){
StringBuilder jsonBody = new StringBuilder("{");
for (Object entry : body.entrySet()){
Map.Entry m = (Map.Entry) entry;
if (m.getValue() instanceof List)
jsonBody.append("\""+m.getKey()+"\":"+JSONStringify((List<Object>) m.getValue()));
if (m.getValue() instanceof Map)
jsonBody.append("\""+m.getKey()+"\":"+JSONStringify((Map<String, Object>) m.getValue()));
if (m.getValue() instanceof Integer)
jsonBody.append("\""+m.getKey()+"\":"+m.getValue());
if (m.getValue() instanceof String)
jsonBody.append("\""+m.getKey()+"\":"+"\""+m.getValue()+"\"");
jsonBody.append(",");
}
int index = jsonBody.lastIndexOf(",");
if (index != -1)jsonBody.delete(index,index+1);
jsonBody.append("}");
return jsonBody.toString();
}
//转换List类型为JSON数组类型
public static String JSONStringify(List<Object> list){
StringBuilder jsonBody = new StringBuilder("[");
for (Object item : list){
if (item instanceof Map)
jsonBody.append(JSONStringify((Map<String, Object>) item));
if (item instanceof Integer)
jsonBody.append(item);
if (item instanceof String)
jsonBody.append("\""+item+"\"");
if (item instanceof List)
jsonBody.append(JSONStringify((List<Object>) item));
jsonBody.append(",");
}
int index = jsonBody.lastIndexOf(",");
if (index != -1)jsonBody.delete(index,index+1);
jsonBody.append("]");
return jsonBody.toString();
}
//post请求,body不传参则传{}
public static void post(String url, String body, Callback callback){
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create( MediaType.parse("application/json; charset=utf-8"),body);
Request request = new Request.Builder()
.url(address+url)
.addHeader("Authorization",TOKEN)
.post(requestBody)
.build();
client.newCall(request).enqueue(callback);
}
//put请求
public static void put(String url, String body, Callback callback){
OkHttpClient client = new OkHttpClient();
RequestBody requestBody =
RequestBody.create( MediaType.parse("application/json; charset=utf-8"),body);
Request request = new Request.Builder()
.url(address+url)
.addHeader("Authorization",TOKEN)
.put(requestBody)
.build();
client.newCall(request).enqueue(callback);
}
//get请求,不传参数则传null
//传参示范:"/1" "?name='1'"
// 也可以直接写在url中,传参传null或""
public static void get(String url, String body, Callback callback){
if (body != null){
url += body;
}
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(address+url)
.addHeader("Authorization",TOKEN)
.get()
.build();
client.newCall(request).enqueue(callback);
}
//delete请求,不传参数则传null
// 传参示范:"/1"
// 也可以直接写在url中,传参传null或""
public static void delete(String url, String body, Callback callback){
if (body != null){
url += body;
}
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(address+url)
.addHeader("Authorization",TOKEN)
.delete()
.build();
client.newCall(request).enqueue(callback);
}
请求参考:
//将传递数据放入Map中
Map<String,Object> data = new HashMap<>();
data.put("username","admin");
data.put("password","123456");
//OkhttpUtil为上面代码的封装类
//实现okhttp3的Callback方法
OkhttpUtil.post("/login", OkhttpUtil.JSONStringify(data), new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("request",e.getLocalizedMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String data = response.body().string();
//存储token
try {
JSONObject jsonObject = new JSONObject(data);
OkhttpUtil.TOKEN = jsonObject.getString("token");
} catch (Exception e) {
Log.e("request",e.getLocalizedMessage());
}
Log.d("request",data);
}
});