Chmath Kalubovila Chmath Kalubovila - 10 months ago 37
JSON Question

How to retrive the json values (string values display as null and integer 0)

i'm implement restful client using android.i have API URL,token,etc.i implement this application using retrofit 2 library all are correct.but json values not display properly.
String values display-null
integer values display-0

this is my code.

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}}


my Mainactivity

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());
}




});
}


my User class

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;
}




}


}

and API class

public interface LoginA {
@GET("/api/user")
Call <User.UserBean> getUserDetails();}


thank you

Answer Source

If your API response is

{"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}}

Then the return type for your call should be User and not User.UserBean