如何使用改进3访问对象内的String值

时间:2017-04-06 12:10:04

标签: java android retrofit2

如何使用改造

访问对象响应中的String结果
{
 "response": {
  "result": "Success",
  "data": "Added Successfully"
 }
}

我的pojo课程

公共类ResponseList {

@SerializedName("response")
private Response response;

public Response getResponse() {
    return response;
}

public void setResponse(Response response) {
    this.response = response;
}

public class ResponseInr {

    @SerializedName("result")
    private String result;
    @SerializedName("data")
    private String data;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

}

}

//发布数据

Map data = new HashMap<>();

    data.put("data_type", "PostCompliant");

    data.put("compliant_name", "map1");

    data.put("location", "map_cbe");

    data.put("description", "map_desc");

    data.put("compliant_status", "map_true");

    data.put("user_id", "map_id");

    Call<ResponseList> call3 = apiInterface.doCreateUserWithFieldrespon(data);
    call3.enqueue(new Callback<ResponseList>() {
        @Override
        public void onResponse(Call<ResponseList> call, Response<ResponseList> response) {

            System.out.println("test" + response.body().toString());

            ResponseList userList = response.body();

            String result = userList.result;
            String data = userList.data;

            responseText.setText(result + "\t" + data);

        }

        @Override
        public void onFailure(Call<ResponseList> call, Throwable t) {

            Log.d("Error", "" + t.getMessage());
            call.cancel();
        }
    });

2 个答案:

答案 0 :(得分:1)

尝试

public class ResponseList {

    @SerializedName("response")
    private ResponseInr response;

    public ResponseInr getResponse() {
        return response;
    }

    public void setResponse(ResponseInr response) {
        this.response = response;
    }

    public class ResponseInr {

        @SerializedName("result")
        private String result;
        @SerializedName("data")
        private String data;

        public String getResult() {
            return result;
        }

        public void setResult(String result) {
            this.result = result;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

    }

}

答案 1 :(得分:1)

通过一些分析,我找到了自己的解决方案:

我的Pojo课程:

public class ResponseList {

@SerializedName("response")
private Response response;

public Response getResponse() {
    return response;
}

public void setResponse(Response response) {
    this.response = response;
}

public class Response {

    @SerializedName("result")
    private String result;
    @SerializedName("data")
    private String data;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

}

}

内部活动

    Call<ResponseList> call3 = apiInterface.doCreateUserWithFieldrespon(data);
    call3.enqueue(new Callback<ResponseList>() {
        @Override
        public void onResponse(Call<ResponseList> call, Response<ResponseList> response) {

            System.out.println("test" + response.body().toString());

            ResponseList userList = response.body();

            ResponseList.Response respo = userList.getResponse();

            String data = respo.getData();
            String result = respo.getResult();

            Toast.makeText(getApplicationContext(), result + " resut\n" + data + " data\n", Toast.LENGTH_SHORT).show();
            responseText.setText(result + "\t" + data);

        }

        @Override
        public void onFailure(Call<ResponseList> call, Throwable t) {

            Log.d("Error", "" + t.getMessage());
            call.cancel();
        }
    });
}