我使用android.i实现restful客户端有API URL,令牌等。使用改造2库实现此应用程序都是正确的。但json值无法正确显示。 字符串值显示为null 整数值display-0
这是我的代码。
json string
{"user": {"login": 22000,"name": "Test Account - C K (Dev)","native_name":ull,"email": null,"identity": null,"password_last_updated_at": null,"phone": null,"balance": 0}}
我的主动性
void getRetrofitObject() {
final String API_BASE_URL = "https://www.example.com/";
final String credentials = "xxxujfrfkjgrtgkmuhefekmneignkdgrg";
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new okhttp3.Interceptor() {
@Override
public Response intercept(okhttp3.Interceptor.Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("Authorization", credentials)
.addHeader("Accept", "application/json").build();
return chain.proceed(request);
}
}).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
LoginA service = retrofit.create(LoginA.class);
Call <User.UserBean> call = service.getUserDetails();
call.enqueue(new Callback <User.UserBean>() {
@Override
public void onResponse(Call <User.UserBean> call, retrofit2.Response <User.UserBean> response) {
try {
text_marks_1.setText("User Balance : " + response.body().getLogin());
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Call <User.UserBean> call, Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
我的用户类
public class User implements Serializable{
/**
* user : {"login":22000,"name":"Test Account - C K (Dev)","native_name":null,"email":null,"identity":null,"password_last_updated_at":null,"phone":null,"balance":0}
*/
private UserBean user;
public UserBean getUser() {
return user;
}
public void setUser(UserBean user) {
this.user = user;
}
public static class UserBean {
/**
* login : 22000
* name : Test Account - C K (Dev)
* native_name : null
* email : null
* identity : null
* password_last_updated_at : null
* phone : null
* balance : 0
*/
private int login;
private String name;
private Object native_name;
private Object email;
private Object identity;
private Object password_last_updated_at;
private Object phone;
private int balance;
public int getLogin() {
return login;
}
public void setLogin(int login) {
this.login = login;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getNative_name() {
return native_name;
}
public void setNative_name(Object native_name) {
this.native_name = native_name;
}
public Object getEmail() {
return email;
}
public void setEmail(Object email) {
this.email = email;
}
public Object getIdentity() {
return identity;
}
public void setIdentity(Object identity) {
this.identity = identity;
}
public Object getPassword_last_updated_at() {
return password_last_updated_at;
}
public void setPassword_last_updated_at(Object password_last_updated_at) {
this.password_last_updated_at = password_last_updated_at;
}
public Object getPhone() {
return phone;
}
public void setPhone(Object phone) {
this.phone = phone;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
}
和API类
public interface LoginA {
@GET("/api/user")
Call <User.UserBean> getUserDetails();}
谢谢
答案 0 :(得分:0)
如果您的API响应是
{"user": {"login": 22000,"name": "Test Account - C K (Dev)","native_name":ull,"email": null,"identity": null,"password_last_updated_at": null,"phone": null,"balance": 0}}
然后,您的调用的返回类型应为User而不是User.UserBean