From 3b6055fa1852e48efad50f2430ff6f87ecc9e3d6 Mon Sep 17 00:00:00 2001 From: eajon <546464955@qq.com> Date: Wed, 23 Jan 2019 16:43:05 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E4=BF=AE=E6=94=B9gson=20=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=9C=AA=E7=9F=A5=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E4=BC=9A=E5=A4=9A=E5=8F=8C=E5=BC=95=E5=8F=B7=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/eajon/annotation/Name.java | 18 ++++ .../github/eajon/exception/ApiException.java | 3 +- .../eajon/function/HttpResponseFunction.java | 8 +- .../java/com/github/eajon/util/GsonUtils.java | 66 +++++++++---- .../main/java/com/eajon/my/MainActivity.java | 17 ++-- .../main/java/com/eajon/my/model/Profile.java | 87 +++++++++++++++++ .../eajon/my/viewModel/WeatherModule2.java | 93 +++++++++++++++++-- 7 files changed, 256 insertions(+), 36 deletions(-) create mode 100644 RxHttp/src/main/java/com/github/eajon/annotation/Name.java create mode 100644 app/src/main/java/com/eajon/my/model/Profile.java diff --git a/RxHttp/src/main/java/com/github/eajon/annotation/Name.java b/RxHttp/src/main/java/com/github/eajon/annotation/Name.java new file mode 100644 index 0000000..cf2277c --- /dev/null +++ b/RxHttp/src/main/java/com/github/eajon/annotation/Name.java @@ -0,0 +1,18 @@ +package com.github.eajon.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Created by threshold on 2017/1/16. + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Name { + String value(); + + boolean require() default true; +} diff --git a/RxHttp/src/main/java/com/github/eajon/exception/ApiException.java b/RxHttp/src/main/java/com/github/eajon/exception/ApiException.java index 1e1a264..a89ee7f 100644 --- a/RxHttp/src/main/java/com/github/eajon/exception/ApiException.java +++ b/RxHttp/src/main/java/com/github/eajon/exception/ApiException.java @@ -92,7 +92,8 @@ public static ApiException handleException(Throwable e) { || e instanceof JsonSyntaxException || e instanceof JsonSerializer || e instanceof NotSerializableException - || e instanceof ParseException) { + || e instanceof ParseException + || e instanceof UnsupportedOperationException) { ex = new ApiException(e, ERROR.PARSE_ERROR); ex.bodyMessage = "解析错误"; return ex; diff --git a/RxHttp/src/main/java/com/github/eajon/function/HttpResponseFunction.java b/RxHttp/src/main/java/com/github/eajon/function/HttpResponseFunction.java index 587d786..8454be7 100644 --- a/RxHttp/src/main/java/com/github/eajon/function/HttpResponseFunction.java +++ b/RxHttp/src/main/java/com/github/eajon/function/HttpResponseFunction.java @@ -14,7 +14,7 @@ * * @author wengyijiong */ -public class HttpResponseFunction implements Function { +public class HttpResponseFunction implements Function { private Type type; @@ -29,7 +29,11 @@ public Object apply(@NonNull JsonElement response) throws Exception { LoggerUtils.json(response.toString()); /*此处不再处理业务相关逻辑交由开发者重写httpCallback*/ if (type == null) { - return new Gson().toJson(response); + if (response.isJsonObject()) { + return response.toString(); + } else { + return response.getAsString(); + } } return new Gson().fromJson(response, type); diff --git a/RxHttp/src/main/java/com/github/eajon/util/GsonUtils.java b/RxHttp/src/main/java/com/github/eajon/util/GsonUtils.java index c3ffbdc..9dea5e3 100644 --- a/RxHttp/src/main/java/com/github/eajon/util/GsonUtils.java +++ b/RxHttp/src/main/java/com/github/eajon/util/GsonUtils.java @@ -1,5 +1,8 @@ package com.github.eajon.util; +import android.text.TextUtils; + +import com.github.eajon.annotation.Name; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; @@ -9,6 +12,7 @@ import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; +import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; @@ -16,30 +20,54 @@ public class GsonUtils { - private static Gson gson = new GsonBuilder() - .registerTypeAdapter( - new TypeToken>() { - }.getType(), - new JsonDeserializer>() { - @Override - public Map deserialize( - JsonElement json, Type typeOfT, - JsonDeserializationContext context) throws JsonParseException { - Map map = new HashMap<>(); - JsonObject jsonObject = json.getAsJsonObject(); - Set> entrySet = jsonObject.entrySet(); - for (Map.Entry entry : entrySet) { - map.put(entry.getKey(), entry.getValue()); - } - return map; - } - }).create(); + public static Gson buildGson(Object object) { + return new GsonBuilder() + .registerTypeAdapter( + new TypeToken>() { + }.getType(), + new MapJsonDeserializer(object)).create(); + } public static Map objectToMap(Object object) { - Map map = gson.fromJson(gson.toJson(object), new TypeToken>() { + Gson gson = buildGson(object); + Map map = gson.fromJson(gson.toJson(object), new TypeToken>() { }.getType()); return map; } + + private static class MapJsonDeserializer implements JsonDeserializer> { + + private Object object; + + private MapJsonDeserializer(Object object) { + this.object = object; + } + + @Override + public Map deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + Map map = new HashMap<>(); + JsonObject jsonObject = json.getAsJsonObject(); + Set> entrySet = jsonObject.entrySet(); + Class clazz = object.getClass(); + for (Map.Entry entry : entrySet) { + try { + Field field = clazz.getDeclaredField(entry.getKey()); + boolean hasName = field.isAnnotationPresent(Name.class); + if (hasName) { + Name name = field.getAnnotation(Name.class); + if (name.require()) { + map.put(TextUtils.isEmpty(name.value()) ? entry.getKey() : name.value(), entry.getValue().isJsonObject() ? entry.getValue().toString() : entry.getValue().getAsString()); + } + } else { + map.put(entry.getKey(), entry.getValue().isJsonObject() ? entry.getValue().toString() : entry.getValue().getAsString()); + } + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } + return map; + } + } } diff --git a/app/src/main/java/com/eajon/my/MainActivity.java b/app/src/main/java/com/eajon/my/MainActivity.java index af9ec6d..b64436b 100644 --- a/app/src/main/java/com/eajon/my/MainActivity.java +++ b/app/src/main/java/com/eajon/my/MainActivity.java @@ -14,6 +14,7 @@ import com.eajon.my.base.BaseActivity; import com.eajon.my.model.BaseResponse; import com.eajon.my.model.CommonResponse; +import com.eajon.my.model.Profile; import com.eajon.my.model.Weather; import com.eajon.my.util.PhotoUtils; import com.eajon.my.util.ZhihuImagePicker; @@ -30,6 +31,7 @@ import com.github.eajon.task.UploadTask; import com.github.eajon.util.LoggerUtils; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import com.qingmei2.rximagepicker.core.RxImagePicker; import com.qingmei2.rximagepicker.entity.Result; import com.qingmei2.rximagepicker_extension.MimeType; @@ -42,6 +44,7 @@ import com.trello.rxlifecycle2.android.ActivityEvent; import java.io.File; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -87,7 +90,7 @@ protected int setContentId() { @Override protected void initView() { - //官方MVVM +// 官方MVVM WeatherModule weatherModule = ViewModelProviders.of(this).get(WeatherModule.class); weatherModule.getWeather().observe(this, new android.arch.lifecycle.Observer() { @Override @@ -95,7 +98,7 @@ public void onChanged(@Nullable Weather weather) { content.setText(new Gson().toJson(weather)); } }); - //RxHttp MVVM +// RxHttp MVVM WeatherModule2 weatherModule2 = ViewModelProviders.of(this).get(WeatherModule2.class); weatherModule2.getWeather(); @@ -172,12 +175,14 @@ private void doProfile() { new RxHttp.Builder() .get() .apiUrl("api/user/profile") - .entity(CommonResponse.class) .build() - .request(new HttpObserver() { + .request(new HttpObserver() { @Override - public void onSuccess(CommonResponse o) { - content.setText(o.getData().toString()); + public void onSuccess(String o) { + Type type = new TypeToken>() { + }.getType(); + CommonResponse profile = new Gson().fromJson(o, type); + content.setText(profile.getData().getNickname()); } @Override diff --git a/app/src/main/java/com/eajon/my/model/Profile.java b/app/src/main/java/com/eajon/my/model/Profile.java new file mode 100644 index 0000000..335b5c9 --- /dev/null +++ b/app/src/main/java/com/eajon/my/model/Profile.java @@ -0,0 +1,87 @@ +package com.eajon.my.model; + +public class Profile { + + private String city; + private String country; + private String headImgUrl; + private int id; + private String language; + private String mobile; + private String name; + private String nickname; + private int sex; + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getHeadImgUrl() { + return headImgUrl; + } + + public void setHeadImgUrl(String headImgUrl) { + this.headImgUrl = headImgUrl; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getNickname() { + return nickname; + } + + public void setNickname(String nickname) { + this.nickname = nickname; + } + + public int getSex() { + return sex; + } + + public void setSex(int sex) { + this.sex = sex; + } +} + diff --git a/app/src/main/java/com/eajon/my/viewModel/WeatherModule2.java b/app/src/main/java/com/eajon/my/viewModel/WeatherModule2.java index 7545b52..dfa410f 100644 --- a/app/src/main/java/com/eajon/my/viewModel/WeatherModule2.java +++ b/app/src/main/java/com/eajon/my/viewModel/WeatherModule2.java @@ -2,26 +2,103 @@ import android.arch.lifecycle.ViewModel; -import com.eajon.my.model.Weather; +import com.eajon.my.model.BaseResponse; import com.github.eajon.RxHttp; - -import java.util.HashMap; +import com.github.eajon.annotation.Name; public class WeatherModule2 extends ViewModel { public void getWeather() { - HashMap map = new HashMap(); - map.put("city", "常熟"); + TestGson testGson = new TestGson(); + testGson.setCity2("常熟"); + testGson.setTest("111"); + testGson.setTest2("222"); + testGson.setCity3("222"); + testGson.setCity4(1); + BaseResponse baseResponse = new BaseResponse(); + baseResponse.setCode(1); + baseResponse.setMessage("HAHAH"); + testGson.setBaseResponse(baseResponse); +// HashMap map = new HashMap(); +// map.put("city", "常熟"); new RxHttp.Builder() .get() .baseUrl("http://wthrcdn.etouch.cn/") .apiUrl("weather_mini") - .addParameter(map) - .entity(Weather.class) + .addObjectParameter(testGson) +// .addParameter(map) +// .entity(Weather.class) .tag("weather") - .cacheKey("HAHAHAHA") .build() .request(); } + + private class TestGson { + + @Name(value = "haha", require = false) + private String test; + + @Name(value = "haha2", require = false) + private String test2; + + @Name(value = "city") + private String city2; + + private String city3; + + private int city4; + + + private BaseResponse baseResponse; + + public BaseResponse getBaseResponse() { + return baseResponse; + } + + public void setBaseResponse(BaseResponse baseResponse) { + this.baseResponse = baseResponse; + } + + public String getTest() { + return test; + } + + public void setTest(String test) { + this.test = test; + } + + public String getTest2() { + return test2; + } + + public void setTest2(String test2) { + this.test2 = test2; + } + + public String getCity2() { + return city2; + } + + public void setCity2(String city2) { + this.city2 = city2; + } + + public String getCity3() { + return city3; + } + + public void setCity3(String city3) { + this.city3 = city3; + } + + public int getCity4() { + return city4; + } + + public void setCity4(int city4) { + this.city4 = city4; + } + + } }