Android - 使用Retrofit 2.1.0的简单JSON对象响应?

时间:2017-01-17 08:09:06

标签: android json retrofit2 android-json

当我连接到网络服务时,它会让我看到json

[
    {"Key":"Sucsess",
    "Value":{
    "Family":"Warton",
    "Name":"James",
    "Role":"student",
    "Status":"true"}
    }
]

如何从此json获取数据?

我使用这个SITE给我两个课:

1- ModelSignIn.class

public class ModelSignIn {
    @SerializedName("Family")
    @Expose
    private String family;
    @SerializedName("Name")
    @Expose
    private String name;
    @SerializedName("Role")
    @Expose
    private String role;
    @SerializedName("Status")
    @Expose
    private String status;

    public String getFamily() {
        return family;
    }

    public void setFamily(String family) {
        this.family = family;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

2- JsonSignIn.class

public class JsonSignIn {
    @SerializedName("Key")
    @Expose
    private String key;
    @SerializedName("Value")
    @Expose
    private ModelSignIn modelSignIn;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public ModelSignIn getValue() {
        return modelSignIn;
    }

    public void setValue(ModelSignIn modelSignIn) {
        this.modelSignIn = modelSignIn;
    }
}

我如何在代码中使用上面的类:

public class ConnectRetrofits {
    private void loadJSONLogin(String username, String password) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ConstUrl.BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Interface_SignIn request = retrofit.create(Interface_SignIn.class);
        Call<ModelSignIn> call = request.getJSONSignIn(username, password);
        call.enqueue(new Callback<ModelSignIn>() {
            @Override
            public void onResponse(Call<ModelSignIn> call, Response<ModelSignIn> response) {
                JsonSignIn JSI = new JsonSignIn();

            }

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

            }
        });
    }
}

Interface_SignIn.class

public interface Interface_SignIn {
    @GET("/GiveData.svc/login/{UserName}/{Password}")
    Call<ModelSignIn> getJSONSignIn(@Path("UserName") String UserName,
                                    @Path("Password") String Password);
}

2 个答案:

答案 0 :(得分:0)

由于您正在使用gson转换器,因此您只需将响应转换为ModelSignIn

 public void onResponse(Call<ModelSignIn> call, Response<ModelSignIn> response) {
                ModelSignIn msi = (ModelSignIn)response.body();
               ...

然后你可以得到元素:

msi.getName();

等...

答案 1 :(得分:0)

使用body()方法获取您的响应模型。在您的情况下,您的请求中的模型不正确。在您的情况下,响应是由于某种原因的数组。父对象是JsonSignIn。因此,您需要相应地更新代码。

public interface Interface_SignIn {
    @GET("/GiveData.svc/login/{UserName}/{Password}")
    Call<List<JsonSignIn>> getJSONSignIn(@Path("UserName") String UserName,
                                         @Path("Password") String Password);
}

不要忘记更新您的请求。

Call<List<JsonSignIn>> call = request.getJSONSignIn(username, password);
    call.enqueue(new Callback<List<JsonSignIn>>() {
        @Override
        public void onResponse(Call<ModelSignIn> call, Response<List<JsonSignIn>> response) {
            List<JsonSignIn> JSI = response.body();

        }

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

        }
    });