Android开发实战(十八):Android Studio 优秀插件:GsonFormat

简介: 原文:Android开发实战(十八):Android Studio 优秀插件:GsonFormatAndroid Studio 优秀插件系列:                       Android Studio 优秀插件(一):GsonFormat                    ...
原文: Android开发实战(十八):Android Studio 优秀插件:GsonFormat

Android Studio 优秀插件系列:

                      Android Studio 优秀插件(一):GsonFormat

                      Android Studio 优秀插件(二): Parcelable Code Generator

 

-------------------------------------------------------------------------------------------------------

这几天没有活,于是乎整理了一些代码,顺便把一些一直在使用的东西也整理下,然后学习新的知识。。

-------------------------------------------------------------------------------------------------------

作为一个Android程序猿,当你看到后台给你的json数据格式时:

{
    "id":123,
    "url": "http://img.donever.com/aa/bb.jpg",
    "width":500,
    "height":500,
    "likeCount":1,
    "description":"嘿嘿",
    "time":1234567890,
    "replyCount":0,
    "floorCount":0,
    "likeUserCount":5,
    "age":14,
    "name":"jack",
    "school":"beijing",
    "type":1,    
    "sax":"boy",
    "userid":1223
}

是不是要默默的创建一个类,然后一个个变量的private 一下,然后get()+set()?

如果一个json数据提供的属性20+条或者30+条呢,一个个属性去写,还要保证字母不写错,大小写也没错,是不是既浪费时间又浪费精力,那么就试试使用GsonFormat插件吧

 

现在学习下如何使用这个插件:

1、Android Studio 打开一个项目,点击左上角 File -->Settings... 进行设置

 

2、选择插件Plugins , 搜索GsonFormat ,如果你没有下载过这个插件,那么搜索框下面会显示“Nothing to show.Click Browse to....”

 

3、那就点击蓝色字体的 Browse 吧  ,这个时候会出现如下图的界面,我们只需要在左边选中GsonFormat 然后点击右面 绿色按钮 "Install plugin" 就可以了

 

4、完成了上面三个步骤,就可以使用GsonFormat插件了

怎么用呢,

(1)创建一个类文件,类名是看你需求自定义写的

(2)快捷键 alt+insert ,会出现如下选择框

(3)我们点击第一个选项,GsonFormat,就会出现一个新的框,

然后只需要将服务器给你的json数据的 格式复制进去 ,如下所示,点击Ok就可以了(注意json格式不要出错,比如不要少了每个属性后面的逗号)

 

(4)最后一步,出现这么一个框,这里你可以进行相应的编辑,比如吧一个属性的类型改变,或者 去掉属性前面的蓝底白勾,让类不具有某个属性

 

效果类:

  1 public class People {
  2 
  3     /**
  4      * id : 123
  5      * url : http://img.donever.com/aa/bb.jpg
  6      * width : 500
  7      * height : 500
  8      * likeCount : 1
  9      * description : 嘿嘿
 10      * time : 1234567890
 11      * replyCount : 0
 12      * floorCount : 0
 13      * likeUserCount : 5
 14      * age : 14
 15      * name : jack
 16      * school : beijing
 17      * type : 1
 18      * sax : boy
 19      * userid : 1223
 20      */
 21 
 22     private int id;
 23     private String url;
 24     private int width;
 25     private int height;
 26     private int likeCount;
 27     private String description;
 28     private int time;
 29     private int replyCount;
 30     private int floorCount;
 31     private int likeUserCount;
 32     private int age;
 33     private String name;
 34     private String school;
 35     private int type;
 36     private String sax;
 37     private int userid;
 38 
 39     public static People objectFromData(String str) {
 40         Gson gson = new Gson();
 41 
 42         return new com.google.gson.Gson().fromJson(str, People.class);
 43     }
 44 
 45     public void setId(int id) {
 46         this.id = id;
 47     }
 48 
 49     public void setUrl(String url) {
 50         this.url = url;
 51     }
 52 
 53     public void setWidth(int width) {
 54         this.width = width;
 55     }
 56 
 57     public void setHeight(int height) {
 58         this.height = height;
 59     }
 60 
 61     public void setLikeCount(int likeCount) {
 62         this.likeCount = likeCount;
 63     }
 64 
 65     public void setDescription(String description) {
 66         this.description = description;
 67     }
 68 
 69     public void setTime(int time) {
 70         this.time = time;
 71     }
 72 
 73     public void setReplyCount(int replyCount) {
 74         this.replyCount = replyCount;
 75     }
 76 
 77     public void setFloorCount(int floorCount) {
 78         this.floorCount = floorCount;
 79     }
 80 
 81     public void setLikeUserCount(int likeUserCount) {
 82         this.likeUserCount = likeUserCount;
 83     }
 84 
 85     public void setAge(int age) {
 86         this.age = age;
 87     }
 88 
 89     public void setName(String name) {
 90         this.name = name;
 91     }
 92 
 93     public void setSchool(String school) {
 94         this.school = school;
 95     }
 96 
 97     public void setType(int type) {
 98         this.type = type;
 99     }
100 
101     public void setSax(String sax) {
102         this.sax = sax;
103     }
104 
105     public void setUserid(int userid) {
106         this.userid = userid;
107     }
108 
109     public int getId() {
110         return id;
111     }
112 
113     public String getUrl() {
114         return url;
115     }
116 
117     public int getWidth() {
118         return width;
119     }
120 
121     public int getHeight() {
122         return height;
123     }
124 
125     public int getLikeCount() {
126         return likeCount;
127     }
128 
129     public String getDescription() {
130         return description;
131     }
132 
133     public int getTime() {
134         return time;
135     }
136 
137     public int getReplyCount() {
138         return replyCount;
139     }
140 
141     public int getFloorCount() {
142         return floorCount;
143     }
144 
145     public int getLikeUserCount() {
146         return likeUserCount;
147     }
148 
149     public int getAge() {
150         return age;
151     }
152 
153     public String getName() {
154         return name;
155     }
156 
157     public String getSchool() {
158         return school;
159     }
160 
161     public int getType() {
162         return type;
163     }
164 
165     public String getSax() {
166         return sax;
167     }
168 
169     public int getUserid() {
170         return userid;
171     }
172 }
People

 如果要使用Gson解析的话 ,即 通过Json字符串直接获取对应的类对象

public static People objectFromData(String str) {
        Gson gson = new Gson();
        return gson.fromJson(str, People.class);
    }

记得在build.gradle 中加上

compile 'com.google.code.gson:gson:2.4'

 

目录
相关文章
|
6天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
1天前
|
Java API 开发工具
java与Android开发入门指南
java与Android开发入门指南
6 0
|
2天前
|
Oracle Java 关系型数据库
Android零基础入门(一)配置环境和安装Android Studio
Android零基础入门(一)配置环境和安装Android Studio
11 0
|
2天前
|
Android开发 Kotlin
Kotlin开发Android之基础问题记录
Kotlin开发Android之基础问题记录
13 1
|
2天前
|
Android开发
Android Studio或IDEA 升级后 不提示错误问题
Android Studio或IDEA 升级后 不提示错误问题
11 1
|
2天前
|
Java Android开发
Android开发@IntDef完美替代Enum
Android开发@IntDef完美替代Enum
10 0
|
3天前
|
Android开发
Android 盒子开发过程中遇到的问题及解决方法
Android 盒子开发过程中遇到的问题及解决方法
7 2
|
3天前
|
机器学习/深度学习 算法 Android开发
安卓应用开发:打造高效通知管理系统
【5月更文挑战第6天】 在现代移动应用的海洋中,用户经常面临信息过载的挑战。一个精心设计的通知管理系统对于提升用户体验至关重要。本文将探讨在安卓平台上如何实现一个高效的通知管理系统,包括最佳实践、系统架构设计以及性能优化技巧。通过分析安卓通知渠道和优先级设置,我们的目标是帮助开发者构建出既能吸引用户注意,又不会引发干扰的智能通知系统。
16 2
|
4天前
|
安全 Linux Android开发
FFmpeg开发笔记(十六)Linux交叉编译Android的OpenSSL库
该文介绍了如何在Linux服务器上交叉编译Android的FFmpeg库以支持HTTPS视频播放。首先,从GitHub下载openssl源码,解压后通过编译脚本`build_openssl.sh`生成64位静态库。接着,更新环境变量加载openssl,并编辑FFmpeg配置脚本`config_ffmpeg_openssl.sh`启用openssl支持。然后,编译安装FFmpeg。最后,将编译好的库文件导入App工程的相应目录,修改视频链接为HTTPS,App即可播放HTTPS在线视频。
FFmpeg开发笔记(十六)Linux交叉编译Android的OpenSSL库
|
9天前
|
存储 Java Android开发
安卓应用开发中的内存优化策略
【4月更文挑战第30天】在移动开发领域,尤其是安卓平台上,内存管理是影响应用性能和用户体验的关键因素。由于安卓设备的硬件资源有限,不合理的内存使用会导致应用响应缓慢、消耗过多电量甚至崩溃。本文将探讨针对安卓平台的内存优化技巧,旨在帮助开发者提高应用的性能和稳定性,从而提升用户满意度。我们将详细讨论内存泄漏的预防、合理的内存分配策略以及高效的内存回收方法。