上一章:Android功能 实现OKHttp网络请求

效果图:

项目结构:

Android功能: 实现GSON解析JSON数据_GSON

导入依赖: implementation 'com.google.code.gson:gson:2.7’
Android功能: 实现GSON解析JSON数据_王睿丶_02
CardBean.java

public class CardBean {
    private int  Balance;               //当前余额
    private int  insertBalcance;        //充值金额
    private String time;                //时间
    private String user="user1";               //用户名
    private String RESULT;              //连接成功提示
    private String ERRMSG;              //连接成功提示

    public CardBean(){
    }

    public CardBean(int balance, int insertBalcance, String time, String user, String RESULT, String ERRMSG) {
        Balance = balance;
        this.insertBalcance = insertBalcance;
        this.time = time;
        this.user = user;
        this.RESULT = RESULT;
        this.ERRMSG = ERRMSG;
    }

    public int getBalance() {
        return Balance;
    }

    public void setBalance(int balance) {
        Balance = balance;
    }

    public int getInsertBalcance() {
        return insertBalcance;
    }

    public void setInsertBalcance(int insertBalcance) {
        this.insertBalcance = insertBalcance;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getRESULT() {
        return RESULT;
    }

    public void setRESULT(String RESULT) {
        this.RESULT = RESULT;
    }

    public String getERRMSG() {
        return ERRMSG;
    }

    public void setERRMSG(String ERRMSG) {
        this.ERRMSG = ERRMSG;
    }
}

OkHttp封装类不变,不知道的请拉到最顶部点击上一章查看

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";   //Log-Name
    private CardBean cardBean = new CardBean();         //新增代码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        queryOkHttp();
    }

    //请求服务器数据
    private void queryOkHttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("UserName", "user1");        //提交数据给服务器-数据1
                    jsonObject.put("CarId",1);                  //提交数据给服务器-数据2
                    final JSONObject result = OkHttp.send("GetCarAccountBalance.do", jsonObject);
                    Log.i(TAG, "run: " + result);   //拿到服务器返回的JSON数据后,打印结果
                    /**
                    *	注意:GSON解析JSON数据的原理是每一个键名要对应 实体bean里的成员变量名
                    *	也就是说名字要一样才能自动映射到对应的成员变量上
                    *	完成后,该bean的映射的成员变量就有值了
                    **/
                    Gson gson = new Gson();                //新增代码
                    cardBean = gson.fromJson(String.valueOf(result),CardBean.class);   //新增代码
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}