Android Development

用OkHttp获取数据,用fastjson解析数据

2017-01-08  本文已影响1318人  喜欢书的女孩
2017-3-21

框架使人懒惰,而我正想偷懒一下。
实现目标:用OkHttp获取数据,用fastjson解析数据

版权声明:本文为博主原创文章,转载请说明出处。

[1]数据源

JSON元素中还有JSON数据

{
  "scanNumber": "1",
  "message": "success",
  "user": {
    "id": "082c9098",
    "serial": "1478571500302eSxpLEBIR9k3HogJQWLI6znTS",
    "createTime": "2016-11-27 16:37:27",
    "updateTime": "2016-11-27 16:37:27",
    "user_id": "f56c827f-027f-49e0-8b35-be3860915537",
    "sex": null,
    "constellation": null
  }
}

[2]获取数据

用OkHttp获取数据(GET方法)
[1]拿到OkHttpClient
[2]构造Request
[3]将Request封装为call
[4]执行call

private  boolean doGet(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    enqueue();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return false;
    }
    private void enqueue(){
        //[1]拿到OkHttpClient
        OkHttpClient client = new OkHttpClient();
        //[2]构造Request 
        Request request = new Request.Builder()
                .url("这里填请求的地址")
                .build();
        //[3]将Request封装为call
        Call call=client.newCall(request);
        //[4]执行call
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                L.i(LOGTAG,"onFailure"+e.getMessage());
                e.printStackTrace();
            }
            @Override
            public void onResponse(Response response) throws IOException {
                res=response.body().string();
                L.i(LOGTAG,"onResponse"+res);
                //ToDo 接下来是解析数据
            }
        });
    }

[3]根据数据源写javaBean

[1]jsonObject里面包含一个userObject
[2]创建两个Bean类
注意:Bean中的变量名必须和返回过来的JSON串里面的参数一样,不一样的变量名将不会有数据。多了或者少了不会有影响。

/**
 *"scanNumber": "1",
 "message": "success",
 "user": {
 "id": "082c9098-0326-4f18-8f5d-5e0494d9c6ab",
 "serial": "1478571500302eSxpLEBIR9k3HogJQWLI6znTS",
 "createTime": "2016-11-27 16:37:27",
 "updateTime": "2016-11-27 16:37:27",
 "user_id": "f56c827f-027f-49e0-8b35-be3860915537",
 "sex": null,
 "constellation": null
 }
 */

public class ScanNumber {
    private String scanNumber;
    private String message;
    private UserBean user;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getScanNumber() {
        return scanNumber;
    }

    public void setScanNumber(String scanNumber) {
        this.scanNumber = scanNumber;
    }

    public UserBean getUser() {
        return user;
    }

    public void setUser(UserBean user) {
        this.user = user;
    }
}
/**
 * "user": {
 "id": "082c9098-0326-4f18-8f5d-5e0494d9c6ab",
 "serial": "1478571500302eSxpLEBIR9k3HogJQWLI6znTS",
 "createTime": "2016-11-27 16:37:27",
 "updateTime": "2016-11-27 16:37:27",
 "user_id": "f56c827f-027f-49e0-8b35-be3860915537",
 "sex": null,
 "constellation": null
 }
 */

public class UserBean {
    private String id;
    private String serial;
    private String createTime;
    private String updateTime;
    private String user_id;
    private String sex;
    private String constellation;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSerial() {
        return serial;
    }

    public void setSerial(String serial) {
        this.serial = serial;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getConstellation() {
        return constellation;
    }

    public void setConstellation(String constellation) {
        this.constellation = constellation;
    }
}

[4]用fastjson解析数据

要想获取到user的信息,需要先映射到大括号里的内容,之后再get

private void jsonParser(String jsonData) {
        try{
            //json数据解析成一个对象
            ScanNumber scanNumber=JSON.parseObject(jsonData,ScanNumber.class);
            UserBean userBean=JSON.parseObject(jsonData,ScanNumber.class).getUser();
            String message = scanNumber.getMessage();
            String user_id=userBean.getUser_id();
            L.i(LOGTAG,"user_id"+user_id);   //打印Log
            L.i("message"," "+scanNumber.getMessage());
        }catch(Exception e){
                e.printStackTrace();
        }
    }

[5]保存数据

private void saveData(String data){
        preferences = getSharedPreferences("count", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("user_id", user_id);
        editor.apply();
        L.i("保存结果", user_id);
    }

大三的这个寒假压力好大,但还是想说寒假快乐。

上一篇下一篇

猜你喜欢

热点阅读