From e10a36bdda8ec14b6fcf795377c84e43aeb0b9ae Mon Sep 17 00:00:00 2001 From: molskqv38 <748067453@qq.com> Date: Sat, 25 Jun 2022 19:25:29 +0800 Subject: [PATCH] ADD file via upload --- ConnectServer.java | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 ConnectServer.java diff --git a/ConnectServer.java b/ConnectServer.java new file mode 100644 index 0000000..f71f41d --- /dev/null +++ b/ConnectServer.java @@ -0,0 +1,69 @@ +package com.example.share2; + +import com.google.gson.Gson; + +import java.util.HashMap; +import java.util.Map; + +import okhttp3.Headers; +import okhttp3.MediaType; +import okhttp3.RequestBody; + +public class ConnectServer { + public Headers getHeaders(Map mapAdd){ + + //头部只有基本参数appSecret,appId,要要想添加其他参数使用 + Map headers_map = new HashMap<>(); + headers_map.put("appSecret", Constants.appSecret); + headers_map.put("appId", Constants.appId); + Headers.Builder builder_header = new Headers.Builder(); + if(!mapAdd.isEmpty()){ + for (String key:mapAdd.keySet()){ + builder_header.add(key, mapAdd.get(key)); + } + } + + for (String key:headers_map.keySet()){ + builder_header.add(key, headers_map.get(key)); + } + Headers headers = builder_header.build(); + return headers; + } + + public RequestBody getJsonBody(Map mapAdd){ + /*Json格式的body*/ + RequestBody r_body = null; + if (!mapAdd.isEmpty()){ + + Gson gson = new Gson(); + String body_json = gson.toJson(mapAdd); + MediaType postrequestjsontype = MediaType.parse("application/json; charset=utf-8"); + r_body = RequestBody.create(postrequestjsontype,body_json); + + } + return r_body; + } + + + public String getBodyParams(Map bodyParams) { + //1.添加请求参数 + //遍历map中所有参数到builder + if (bodyParams != null && bodyParams.size() > 0) { + StringBuffer stringBuffer = new StringBuffer("?"); + for (String key : bodyParams.keySet()) { + if (bodyParams.get(key) != null) {//如果参数不是null,就拼接起来 + stringBuffer.append("&"); + stringBuffer.append(key); + stringBuffer.append("="); + stringBuffer.append(bodyParams.get(key)); + } + } + + return stringBuffer.toString(); + } else { + return ""; + } + } + + +}