This is the 11th day of my participation in the August More text Challenge. For details, see: August More Text Challenge.

In the Process of Android development, multiple Javabeans will be created. When the App network requests more, a list of data needs to create a new Javabeans. Also, many repetitive methods in Javabeans need to be written again, which is a waste of development time. In this post, I will share the steps to use the GsonFormat plug-in for Android Studio.

The principle of using GsonFormat is the process of converting the JSON data returned by the server into a concrete entity class after the successful request to the server directly based on the network interface.

Download the GsonFormat plug-in

Now introduce the latest entrance to open the download plug-in, those tutorials on the Internet are really not flattering, waste of time or wrong. The specific steps are as follows:

1, Open Android Studio –> Preferences –> Plugins –> Search GsonFormat–> Install

2. After the installation is successful, restart Android Studio.

Use the GsonFormat plug-in to automatically generate beans

First, you need to create an empty JavaBean class

2. Request interface

According to the JSON data returned by the interface, copy all the JSON data directly and wait for the paste

Open the new empty JavaBean class

Then use the shortcut key combination “Control + Enter” and open a menu bar dialog box. Find the “GsonFormat” option in the dialog box and click open.

4. “Open”GsonFormat“, a pop-up box will appear

Paste the json data into the input box and click the “OK” button.

5. A field data format field is displayed

The field code is automatically generated according to the JSON format, and then according to the actual situation, check and remove the unnecessary fields, and finally click the confirm button.

Third, the appendix

1. Json network data requested by the interface

{” code “: 200,” data “: {” deviceNo” : “OT0000033”, “pkId” : “0909086528”}, “info” : “”}

2. Automatically generated beans

public class LoginBean extends Base { private DataBean data; public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class DataBean { private String deviceNo; private String pkId; public String getDeviceNo() { return deviceNo; } public void setDeviceNo(String deviceNo) { this.deviceNo = deviceNo; } public String getPkId() { return pkId; } public void setPkId(String pkId) { this.pkId = pkId; }}}Copy the code

The last

The above is all of this chapter, welcome to pay attention to three shopkeeper’s wechat public number “program ape by three shopkeeper”, three shopkeeper’s Sina Weibo “three shopkeeper 666”, welcome to pay attention!