#1157 增加网络检测接口

This commit is contained in:
billytomato
2019-08-15 13:26:43 +08:00
committed by Binary Wang
parent 6e1d7fcef0
commit d1586aec25
8 changed files with 197 additions and 0 deletions

View File

@@ -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() {

View File

@@ -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;
}
}