通过Gson解析数据后没有任何内容

问题:通过Gson解析数据后,获取解析到的对象中的内容,没有任何数据,控制台也没有任何有关异常或错误的输出

如:

 Type listType = new TypeToken<ArrayList<OpenEntity>>(){}.getType();
 ArrayList<OpenEntity> openUrlEntities= gson.fromJson(mess,listType);
集合长度大于0,但是循环遍历对象中的所有内容都为null


原因:打正式包时对数据进行了混淆



解决方法:

在混淆文件中添加如下代码

#proguard for gson
-keepattributes Signature
-keep class sun.misc.Unsafe { *; }
 -keep class com.google.gson.examples.android.model.** { *; }
 -keep class xxx.xxx.xxx.model.** {*;}

xxx.xxx.xxx.model为保存对象的类的完整的包名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些参考步骤: 1. 首先,您需要在您的应用程序中添加一个用于输入城市名称的文本框和一个按钮,用于触发城市更改操作。您可以使用Android Studio中的布局编辑器来完成这些任务。 2. 创建一个名为“Weather”(或其他您喜欢的名称)的Java类来存储天气数据。在这个类中,您需要定义一个名为“city”的字符串属性,用于存储当前城市的名称。例如: ``` public class Weather { private String city; public String getCity() { return city; } public void setCity(String city) { this.city = city; } } ``` 3. 在您的应用程序中,使用Gson库来解析天气数据。您需要在您的build.gradle文件中添加以下依赖项: ``` implementation 'com.google.code.gson:gson:2.8.6' ``` 4. 在您的应用程序中,创建一个名为“WeatherUtils”的Java类,并添加一个名为“updateWeather”的静态方法,用于更新天气数据。这个方法应该接受一个城市名称作为参数,并使用Gson库从一个天气API获取城市的天气数据。 ``` public class WeatherUtils { public static Weather updateWeather(String cityName) { String apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=" + cityName + "&appid=YOUR_API_KEY_HERE"; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(apiUrl) .build(); try { Response response = client.newCall(request).execute(); String responseData = response.body().string(); Gson gson = new Gson(); Weather weather = gson.fromJson(responseData, Weather.class); weather.setCity(cityName); return weather; } catch (IOException e) { e.printStackTrace(); return null; } } } ``` 5. 在您的应用程序中,当用户点击“更改城市”按钮,调用“updateWeather”方法以获取新城市的天气数据。例如: ``` String newCityName = ...; // 获取用户输入的新城市名称 Weather newWeather = WeatherUtils.updateWeather(newCityName); ``` 6. 最后,将新的天气数据更新到您的应用程序界面中。例如,您可以将新城市的名称显示在文本框中,并在屏幕上显示新城市的天气预报。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值