删除 Request.java
parent
d0456431d6
commit
c0fa4b79fb
|
|
@ -1,57 +0,0 @@
|
||||||
package com.kimgo.wepush.request;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.kimgo.wepush.response.QyWeChatSendMessageApiResponse;
|
|
||||||
import okhttp3.MediaType;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.Response;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Request {
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(Request.class);
|
|
||||||
|
|
||||||
public <T> QyWeChatSendMessageApiResponse okhttpRequest(String url,String accessToken,T object){
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
|
||||||
|
|
||||||
// 使用Jackson进行序列化
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
String jsonBody = null;
|
|
||||||
try {
|
|
||||||
jsonBody = objectMapper.writeValueAsString(object);
|
|
||||||
logger.info("jsonBody: {}",jsonBody);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
logger.error("JSON processing error", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
// 构建请求体
|
|
||||||
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonBody);
|
|
||||||
okhttp3.Request request = new okhttp3.Request.Builder().url(url).post(body).build();
|
|
||||||
try (Response response = client.newCall(request).execute()) {
|
|
||||||
String responseBody = response.body().string();
|
|
||||||
logger.info("request sendMessage api ResponseBody: {}", responseBody);
|
|
||||||
|
|
||||||
ObjectMapper objectMapper1 = new ObjectMapper();
|
|
||||||
QyWeChatSendMessageApiResponse apiResponse = objectMapper1.readValue(responseBody, QyWeChatSendMessageApiResponse.class);
|
|
||||||
|
|
||||||
if (apiResponse.getErrcode() == 0) {
|
|
||||||
logger.debug("Request qyWeChat Success.");
|
|
||||||
return apiResponse;
|
|
||||||
} else if (apiResponse.getErrcode() == 42001 || apiResponse.getErrcode() == 40014) {
|
|
||||||
logger.info("Access token expired. Refreshing token...");
|
|
||||||
// 一个方法来刷新accessToken
|
|
||||||
return apiResponse;
|
|
||||||
} else {
|
|
||||||
// 处理其他错误情况
|
|
||||||
logger.error("Error: {}", apiResponse.getErrmsg());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("OkHttp request error", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue