Solution 1 :
It would depend on the return value you have selected from the retrofit call.
For example, converting that response to a POJO the POJO would be
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("success")
@Expose
private Success success;
public Success getSuccess() {
return success;
}
public void setSuccess(Success success) {
this.success = success;
}
}
-----------------------------------com.example.Success.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Success {
@SerializedName("token")
@Expose
private String token;
@SerializedName("name")
@Expose
private String name;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
So your network call would be
@POST("api") Call<Example> Login(@Body LoginRequest loginRequest);
To access this it would look like (pseudo)
public static void login(String uname, String pword) {
Call<Example> getDataResponseSingle = retroInterface.Login(new LoginRequest(uname, pword));
getDataResponseSingle.enqueue()
new Call<Example>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(Example dataResponse) {
dataResponse.getSuccess().getToken;
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
});
}
You can also look at: http://www.jsonschema2pojo.org/ to create you own POJO
I you are returning directly a JSON Object
e.g Call<JSONObject> getDataResponseSingle = retroInterface.Login(new LoginRequest(uname, pword));
Where you see the successful response you access the inner class (pseudo)
JSONObject main = response.body();
JSONObject success = main.getJSONObject("success");
Problem :
I’m using Retrofit, and I want to get this type of object in Android. Can any one explain how can I get? I can get successfully a single simple object, but when it is inside an object, I’m getting response body null.
Here is the JSON
{
"success": {
"token": "djhfeieryueyjsdheirydjalbbvcxgdgfhjdgs",
"name": "abc"
}
}