Android使用Gson库处理服务端Json数据(Tomcat)

我这个项目是一个自己写的Json文件,然后读取文件内容的天气显示App

  1. 数据准备,这个可以参考博主的上一篇文章https://blog.csdn.net/xiaobai_caoyu/article/details/116938420spm=1001.2014.3001.5501
    然后把json文件复制到你的tomact文件包的webapps/ROOT目录下。具体可以参考:
    https://blog.csdn.net/weixin_43838785/article/details/104236782?utm_term=tomcat%E9%85%8D%E7%BD%AEjson&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allsobaiduweb~default-1-104236782&spm=3001.4430
    Tomcat的下载安装自己可以百度。
    2.导入Gson.jar包,步骤如下:
    选中项目,点击鼠标右键,选择Open Module Setting,进入Project Structure界面,选择Dependencies,选择app,然后点击右侧的方框中的+,Library Dependency,进入Add Library Dependency界面,输入com.google.code.gson:gson,点击Search,选择你需要的版本即可。
    3.导入Okhttp3.jar包,步骤如下:
    选中项目,点击鼠标右键,选择Open Module Setting,进入Project Structure界面,选择Dependencies,选择app,然后点击右侧的方框中的+,Library Dependency,进入Add Library Dependency界面,输入com.squareup.okhttp3:okhttp,点击Search,选择你需要的版本即可。
    4.准备读取本本地的 json 数据文件并解析 json 数据的工具类JsonUtils.java
public class JsonUtils {
   
    private static JsonUtils instance;   // 单例模式

    private JsonUtils(){
   
    }

    public static JsonUtils getInstance(){
   
        if(instance==null){
   
            instance = new JsonUtils();
        }
        return instance;
    }

    /**
     * 读取输入文件内容
     * @param is 输入流
     * @return json 字符串
     */
    private String read(InputStream is){
   
        BufferedReader reader = null;   // 带缓存的读取器
        StringBuilder sb = null;      // 字符串构造器

        String line = null;   // 过程变量

        try{
   
            sb = new StringBuilder();
            // 从 is 中读取内容
            reader = new BufferedReader(new InputStreamReader(is));
            while((line = reader.readLine()) != null){
   
                sb.append(line);
                sb.append("\n");
            }
        }catch(Exception ex){
   
            ex.printStackTrace();
        }finally {
   
            try{
   
                if(reader!=null) reader.close();
                if(is!=null) is.close();
            }catch(Exception ex){
   
                ex.printStackTrace();
            }
        }

        return sb.toString();
    }
 /**
     * 1. 在 JsonUtils 类中添加一个读取方法
     * 从数据文件中读取json数据
     * @param context 上下文信息
     * @param json 天气 json 字符串
     * @return List<Weather> 城市天气数据集合
     */
    public List<Weather> getWeatherListFromJson(Context context, String json){
   
        List<Weather> list = new ArrayList<>();

        Gson gson = new Gson();
        // 通过反射机制,定义一类型解析器
        Type listType = new TypeToken<List<Weather>>(){
   }.getType();
        // 使用 gson 库实例,解析json 字符串,交将结果存入 list 中去
        list = gson.fromJson(json, listType);

        return list;
    }
}
  1. 准备实体类Weather.java
public class Weather {
   
    private String city;     // 城市
    private String weather;  // 天气
    private String temp;     // 气温
    private String pm;       // PM 值
    private String wind;     // 风力

    public Weather() {
   
    }

    public Weather(String city, String weather, String temp, String pm, String wind) {
   
        this.city = city;
        this.weather = weather;
        this.temp = temp;
        this.pm = pm;
        this.wind = wind;
    }

    public String getCity() {
   
        return city;
    }

    public void setCity(String city) {
   
        this.city = city;
    }

    public String getWeather() {
   
        return weather;
    }

    public 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值