mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-20 18:47:38 +08:00
#1157 增加网络检测接口
This commit is contained in:
@@ -380,4 +380,19 @@ public class WxConsts {
|
||||
public static final String VIDEO = "video";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 网络检测入参.
|
||||
*/
|
||||
public static class NetCheckArgs {
|
||||
public static final String ACTIONDNS = "dns";
|
||||
public static final String ACTIONPING = "ping";
|
||||
public static final String ACTIONALL = "all";
|
||||
public static final String OPERATORUNICOM = "UNICOM";
|
||||
public static final String OPERATORCHINANET = "CHINANET";
|
||||
public static final String OPERATORCAP = "CAP";
|
||||
public static final String OPERATORDEFAULT = "DEFAULT";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,43 @@
|
||||
package me.chanjar.weixin.common.bean;
|
||||
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 网络检测.
|
||||
* @author billytomato
|
||||
*/
|
||||
@Data
|
||||
public class WxNetCheckResult implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6918924418847404172L;
|
||||
|
||||
private List<WxNetCheckDnsInfo> dnsInfos = new ArrayList<>();
|
||||
private List<WxNetCheckPingInfo> pingInfos = new ArrayList<>();
|
||||
|
||||
public static WxNetCheckResult fromJson(String json) {
|
||||
return WxGsonBuilder.create().fromJson(json, WxNetCheckResult.class);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class WxNetCheckDnsInfo implements Serializable{
|
||||
private static final long serialVersionUID = 82631178029516008L;
|
||||
private String ip;
|
||||
private String realOperator;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class WxNetCheckPingInfo implements Serializable{
|
||||
private static final long serialVersionUID = -1871970825359178319L;
|
||||
private String ip;
|
||||
private String fromOperator;
|
||||
private String packageLoss;
|
||||
private String time;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,7 @@ package me.chanjar.weixin.common.util.json;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.bean.WxNetCheckResult;
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenu;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
@@ -21,6 +22,8 @@ public class WxGsonBuilder {
|
||||
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMenu.class, new WxMenuGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxMediaUploadResultAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxNetCheckResult.class, new WxNetCheckResultGsonAdapter());
|
||||
|
||||
}
|
||||
|
||||
public static Gson create() {
|
||||
|
@@ -0,0 +1,51 @@
|
||||
package me.chanjar.weixin.common.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.bean.WxNetCheckResult;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author billytomato
|
||||
*/
|
||||
public class WxNetCheckResultGsonAdapter implements JsonDeserializer<WxNetCheckResult> {
|
||||
|
||||
|
||||
@Override
|
||||
public WxNetCheckResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
WxNetCheckResult result = new WxNetCheckResult();
|
||||
|
||||
JsonArray dnssJson = json.getAsJsonObject().get("dns").getAsJsonArray();
|
||||
List<WxNetCheckResult.WxNetCheckDnsInfo> dnsInfoList = new ArrayList<>();
|
||||
if (dnssJson != null && dnssJson.size() > 0) {
|
||||
for (int i = 0; i < dnssJson.size(); i++) {
|
||||
JsonObject buttonJson = dnssJson.get(i).getAsJsonObject();
|
||||
WxNetCheckResult.WxNetCheckDnsInfo dnsInfo = new WxNetCheckResult.WxNetCheckDnsInfo();
|
||||
dnsInfo.setIp(GsonHelper.getString(buttonJson, "ip"));
|
||||
dnsInfo.setRealOperator(GsonHelper.getString(buttonJson, "real_operator"));
|
||||
dnsInfoList.add(dnsInfo);
|
||||
}
|
||||
}
|
||||
|
||||
JsonArray pingsJson = json.getAsJsonObject().get("ping").getAsJsonArray();
|
||||
List<WxNetCheckResult.WxNetCheckPingInfo> pingInfoList = new ArrayList<>();
|
||||
if (pingsJson != null && pingsJson.size() > 0) {
|
||||
for (int i = 0; i < pingsJson.size(); i++) {
|
||||
JsonObject pingJson = pingsJson.get(i).getAsJsonObject();
|
||||
WxNetCheckResult.WxNetCheckPingInfo pingInfo = new WxNetCheckResult.WxNetCheckPingInfo();
|
||||
pingInfo.setIp(GsonHelper.getString(pingJson, "ip"));
|
||||
pingInfo.setFromOperator(GsonHelper.getString(pingJson, "from_operator"));
|
||||
pingInfo.setPackageLoss(GsonHelper.getString(pingJson, "package_loss"));
|
||||
pingInfo.setTime(GsonHelper.getString(pingJson, "time"));
|
||||
pingInfoList.add(pingInfo);
|
||||
}
|
||||
}
|
||||
result.setDnsInfos(dnsInfoList);
|
||||
result.setPingInfos(pingInfoList);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user