【发布时间】:2020-09-15 02:24:47
【问题描述】:
我知道这个问题被问了很多,但我看到的答案都没有帮助我澄清这个问题。 我需要更多关于我的方法的指导,而不是具体的答案。
问题...
我从我的网络服务中收到以下答案之一,具体取决于是否创建了某个用户:
{
"ok": [
{
"id": 1,
"username": "test",
"pass": "123",
"reg_date": "2020-05-27 00:00:00"
}
]
}
还有……
{
"error": [
{
"message": "No such user"
}
]
}
如果我对这个定义有误,请纠正我:两者都是以String 作为键和以array 作为值的对象,而array 可以是任何长度,在这种情况下,类型为object
所以,正如您可能已经猜到的那样,我需要根据场景从该响应中获取特定值,甚至是 key 值(“ok”或“error”),并在某些 TextView 中显示如下内容:
好的, 编号:1
或
错误, 没有这样的用户
到目前为止,我尝试的是为接收到的列表中的每个对象创建一个类:
public class EntityUser {
@SerializedName("id")
private int id;
@SerializedName("username")
private String username;
@SerializedName("pass")
private String password;
@SerializedName("reg_date")
private String registerDate;
//Getters and constructor...
}
public class EntityError {
@SerializedName("message")
private String message;
//Getters and constructor...
}
还有一个 Api,我认为这是问题所在:
public interface JsonApi {
@GET("SelectUser.php")
Call<Map<String, List>> getGenericResponse();
}
正如我上面提到的,我想当Call 被执行时,我得到的响应是Map<String, List>,但我不知道如何处理这个,所以我可以得到我想要的值希望:
Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.35/app_android/webservices/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
JsonApi jsonApi = retrofit.create(JsonApi.class);
Call<Map<String, List>> call = jsonApi.getGenericResponse();
call.enqueue(new Callback<Map<String, List>>() {
@Override
public void onResponse(Call<Map<String, List>> call, Response<Map<String, List>> response) {
if(response.isSuccessful()){
if (response.body().containsKey("ok")){
//Here i need to create instance of EntityUsr so I can bind the values to his vars
}if(response.body().containsKey("error")){
//Shows an error message according to the message value
}
}
}
@Override
public void onFailure(Call<Map<String, List>> call, Throwable t) {
prueba.setText("Error: " + t.getMessage());
}
});
正如我在代码中所写,我需要根据response 从网络服务带来的答案创建相应对象的实例,以便从中获取特定值。
我该怎么做?
考虑到 Retrofit 的可用性,我是否迷失了自己的方法?
我不想改变从 webservice 接收到的 json 的结构
提前致谢!!
【问题讨论】:
标签: java android gson retrofit2