Parsing Json Using Retrofit gives null
I am trying to get data from Json using Retrofit2, but I am getting null.
I am not sure what should I put in Interface GET method?
Json Datum response:
public class DatumResponse {
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("total")
@Expose
private Integer total;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
Retrofit class
public class JsonApi {
public static final String BASE_URL = "https://api.deezer.com";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
And my Interface:
public interface IDatum {
@GET("chart")
Call<DatumResponse> getDatum();
Thanks a lot.
java android json retrofit2
|
show 8 more comments
I am trying to get data from Json using Retrofit2, but I am getting null.
I am not sure what should I put in Interface GET method?
Json Datum response:
public class DatumResponse {
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("total")
@Expose
private Integer total;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
Retrofit class
public class JsonApi {
public static final String BASE_URL = "https://api.deezer.com";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
And my Interface:
public interface IDatum {
@GET("chart")
Call<DatumResponse> getDatum();
Thanks a lot.
java android json retrofit2
1
are you getting response null or some data null? Your interface seems ok if you are aspecting some response in DatumResponse format calling this url api.deezer.com/charts i thinks you are missing last "/" on BASE_URL
– justo
Nov 22 '18 at 9:21
1
changehttps://api.deezer.com
tohttps://api.deezer.com/
– Ali Ahmed
Nov 22 '18 at 9:26
I am getting null, I need to get top 10 tracks from json.
– Toni0123
Nov 22 '18 at 9:26
@AliAhmed Added but still nothing
– Toni0123
Nov 22 '18 at 9:27
your JSON Response is not right. you should add all model classes and then get tracks out of them
– Ali Ahmed
Nov 22 '18 at 9:29
|
show 8 more comments
I am trying to get data from Json using Retrofit2, but I am getting null.
I am not sure what should I put in Interface GET method?
Json Datum response:
public class DatumResponse {
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("total")
@Expose
private Integer total;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
Retrofit class
public class JsonApi {
public static final String BASE_URL = "https://api.deezer.com";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
And my Interface:
public interface IDatum {
@GET("chart")
Call<DatumResponse> getDatum();
Thanks a lot.
java android json retrofit2
I am trying to get data from Json using Retrofit2, but I am getting null.
I am not sure what should I put in Interface GET method?
Json Datum response:
public class DatumResponse {
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("total")
@Expose
private Integer total;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
Retrofit class
public class JsonApi {
public static final String BASE_URL = "https://api.deezer.com";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
And my Interface:
public interface IDatum {
@GET("chart")
Call<DatumResponse> getDatum();
Thanks a lot.
java android json retrofit2
java android json retrofit2
asked Nov 22 '18 at 9:15
Toni0123Toni0123
3517
3517
1
are you getting response null or some data null? Your interface seems ok if you are aspecting some response in DatumResponse format calling this url api.deezer.com/charts i thinks you are missing last "/" on BASE_URL
– justo
Nov 22 '18 at 9:21
1
changehttps://api.deezer.com
tohttps://api.deezer.com/
– Ali Ahmed
Nov 22 '18 at 9:26
I am getting null, I need to get top 10 tracks from json.
– Toni0123
Nov 22 '18 at 9:26
@AliAhmed Added but still nothing
– Toni0123
Nov 22 '18 at 9:27
your JSON Response is not right. you should add all model classes and then get tracks out of them
– Ali Ahmed
Nov 22 '18 at 9:29
|
show 8 more comments
1
are you getting response null or some data null? Your interface seems ok if you are aspecting some response in DatumResponse format calling this url api.deezer.com/charts i thinks you are missing last "/" on BASE_URL
– justo
Nov 22 '18 at 9:21
1
changehttps://api.deezer.com
tohttps://api.deezer.com/
– Ali Ahmed
Nov 22 '18 at 9:26
I am getting null, I need to get top 10 tracks from json.
– Toni0123
Nov 22 '18 at 9:26
@AliAhmed Added but still nothing
– Toni0123
Nov 22 '18 at 9:27
your JSON Response is not right. you should add all model classes and then get tracks out of them
– Ali Ahmed
Nov 22 '18 at 9:29
1
1
are you getting response null or some data null? Your interface seems ok if you are aspecting some response in DatumResponse format calling this url api.deezer.com/charts i thinks you are missing last "/" on BASE_URL
– justo
Nov 22 '18 at 9:21
are you getting response null or some data null? Your interface seems ok if you are aspecting some response in DatumResponse format calling this url api.deezer.com/charts i thinks you are missing last "/" on BASE_URL
– justo
Nov 22 '18 at 9:21
1
1
change
https://api.deezer.com
to https://api.deezer.com/
– Ali Ahmed
Nov 22 '18 at 9:26
change
https://api.deezer.com
to https://api.deezer.com/
– Ali Ahmed
Nov 22 '18 at 9:26
I am getting null, I need to get top 10 tracks from json.
– Toni0123
Nov 22 '18 at 9:26
I am getting null, I need to get top 10 tracks from json.
– Toni0123
Nov 22 '18 at 9:26
@AliAhmed Added but still nothing
– Toni0123
Nov 22 '18 at 9:27
@AliAhmed Added but still nothing
– Toni0123
Nov 22 '18 at 9:27
your JSON Response is not right. you should add all model classes and then get tracks out of them
– Ali Ahmed
Nov 22 '18 at 9:29
your JSON Response is not right. you should add all model classes and then get tracks out of them
– Ali Ahmed
Nov 22 '18 at 9:29
|
show 8 more comments
1 Answer
1
active
oldest
votes
Add complete json to JsonScheme2pojo you will get these classes
Here TrackDetailsModel is Main Model Class. using this you can get other data.
Now change
@GET("chart")
Call<DatumResponse> getDatum();
To
@GET("chart")
Call<TrackDetailsModel> getDatum();
Get data using this
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<TrackDetailsModel> call = apiInterface.getDatum();
call.enqueue(new Callback<TrackDetailsModel>() {
@Override
public void onResponse(Call<TrackDetailsModel> call, Response<TrackDetailsModel> response) {
if (response.isSuccessful()) {
TrackDetailsModel model = response.body();
String trackTitle = model.getTracks().getData().getTitle();
}
}
@Override
public void onFailure (Call <TrackDetailsModel> call, Throwable t){
call.cancel();
Toast.makeText(Main.this,"Error: " + t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
I am not sure, but adding my json does not provide me all that. api.deezer.com/chart
– Toni0123
Nov 22 '18 at 9:51
have you added JSON got fromapi.deezer.com/chart
tojsonScheme2pojo
?
– Ali Ahmed
Nov 22 '18 at 9:53
@Toni0123 when you open jsonschema2pojo there will beClass name
on Right Top, Change that from Example to TrackDetailsModel . Then add your all json (that you got fromapi.deezer.com/chart
to Text Section on Left.) Must Remember Source Type should be JSON and Annotation style should be GSON. Then at bottom press Preview. you will get all Model Classes. add those to your project
– Ali Ahmed
Nov 22 '18 at 9:59
Thanks, I will try :)
– Toni0123
Nov 22 '18 at 10:20
Tried, but it does not work. Call<TrackDetailsModel> call = IApiClient.getDatum(); needs to be static,
– Toni0123
Nov 22 '18 at 11:35
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427428%2fparsing-json-using-retrofit-gives-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add complete json to JsonScheme2pojo you will get these classes
Here TrackDetailsModel is Main Model Class. using this you can get other data.
Now change
@GET("chart")
Call<DatumResponse> getDatum();
To
@GET("chart")
Call<TrackDetailsModel> getDatum();
Get data using this
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<TrackDetailsModel> call = apiInterface.getDatum();
call.enqueue(new Callback<TrackDetailsModel>() {
@Override
public void onResponse(Call<TrackDetailsModel> call, Response<TrackDetailsModel> response) {
if (response.isSuccessful()) {
TrackDetailsModel model = response.body();
String trackTitle = model.getTracks().getData().getTitle();
}
}
@Override
public void onFailure (Call <TrackDetailsModel> call, Throwable t){
call.cancel();
Toast.makeText(Main.this,"Error: " + t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
I am not sure, but adding my json does not provide me all that. api.deezer.com/chart
– Toni0123
Nov 22 '18 at 9:51
have you added JSON got fromapi.deezer.com/chart
tojsonScheme2pojo
?
– Ali Ahmed
Nov 22 '18 at 9:53
@Toni0123 when you open jsonschema2pojo there will beClass name
on Right Top, Change that from Example to TrackDetailsModel . Then add your all json (that you got fromapi.deezer.com/chart
to Text Section on Left.) Must Remember Source Type should be JSON and Annotation style should be GSON. Then at bottom press Preview. you will get all Model Classes. add those to your project
– Ali Ahmed
Nov 22 '18 at 9:59
Thanks, I will try :)
– Toni0123
Nov 22 '18 at 10:20
Tried, but it does not work. Call<TrackDetailsModel> call = IApiClient.getDatum(); needs to be static,
– Toni0123
Nov 22 '18 at 11:35
|
show 3 more comments
Add complete json to JsonScheme2pojo you will get these classes
Here TrackDetailsModel is Main Model Class. using this you can get other data.
Now change
@GET("chart")
Call<DatumResponse> getDatum();
To
@GET("chart")
Call<TrackDetailsModel> getDatum();
Get data using this
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<TrackDetailsModel> call = apiInterface.getDatum();
call.enqueue(new Callback<TrackDetailsModel>() {
@Override
public void onResponse(Call<TrackDetailsModel> call, Response<TrackDetailsModel> response) {
if (response.isSuccessful()) {
TrackDetailsModel model = response.body();
String trackTitle = model.getTracks().getData().getTitle();
}
}
@Override
public void onFailure (Call <TrackDetailsModel> call, Throwable t){
call.cancel();
Toast.makeText(Main.this,"Error: " + t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
I am not sure, but adding my json does not provide me all that. api.deezer.com/chart
– Toni0123
Nov 22 '18 at 9:51
have you added JSON got fromapi.deezer.com/chart
tojsonScheme2pojo
?
– Ali Ahmed
Nov 22 '18 at 9:53
@Toni0123 when you open jsonschema2pojo there will beClass name
on Right Top, Change that from Example to TrackDetailsModel . Then add your all json (that you got fromapi.deezer.com/chart
to Text Section on Left.) Must Remember Source Type should be JSON and Annotation style should be GSON. Then at bottom press Preview. you will get all Model Classes. add those to your project
– Ali Ahmed
Nov 22 '18 at 9:59
Thanks, I will try :)
– Toni0123
Nov 22 '18 at 10:20
Tried, but it does not work. Call<TrackDetailsModel> call = IApiClient.getDatum(); needs to be static,
– Toni0123
Nov 22 '18 at 11:35
|
show 3 more comments
Add complete json to JsonScheme2pojo you will get these classes
Here TrackDetailsModel is Main Model Class. using this you can get other data.
Now change
@GET("chart")
Call<DatumResponse> getDatum();
To
@GET("chart")
Call<TrackDetailsModel> getDatum();
Get data using this
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<TrackDetailsModel> call = apiInterface.getDatum();
call.enqueue(new Callback<TrackDetailsModel>() {
@Override
public void onResponse(Call<TrackDetailsModel> call, Response<TrackDetailsModel> response) {
if (response.isSuccessful()) {
TrackDetailsModel model = response.body();
String trackTitle = model.getTracks().getData().getTitle();
}
}
@Override
public void onFailure (Call <TrackDetailsModel> call, Throwable t){
call.cancel();
Toast.makeText(Main.this,"Error: " + t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
Add complete json to JsonScheme2pojo you will get these classes
Here TrackDetailsModel is Main Model Class. using this you can get other data.
Now change
@GET("chart")
Call<DatumResponse> getDatum();
To
@GET("chart")
Call<TrackDetailsModel> getDatum();
Get data using this
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<TrackDetailsModel> call = apiInterface.getDatum();
call.enqueue(new Callback<TrackDetailsModel>() {
@Override
public void onResponse(Call<TrackDetailsModel> call, Response<TrackDetailsModel> response) {
if (response.isSuccessful()) {
TrackDetailsModel model = response.body();
String trackTitle = model.getTracks().getData().getTitle();
}
}
@Override
public void onFailure (Call <TrackDetailsModel> call, Throwable t){
call.cancel();
Toast.makeText(Main.this,"Error: " + t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
edited Nov 22 '18 at 11:38
answered Nov 22 '18 at 9:40
Ali AhmedAli Ahmed
1,2851314
1,2851314
I am not sure, but adding my json does not provide me all that. api.deezer.com/chart
– Toni0123
Nov 22 '18 at 9:51
have you added JSON got fromapi.deezer.com/chart
tojsonScheme2pojo
?
– Ali Ahmed
Nov 22 '18 at 9:53
@Toni0123 when you open jsonschema2pojo there will beClass name
on Right Top, Change that from Example to TrackDetailsModel . Then add your all json (that you got fromapi.deezer.com/chart
to Text Section on Left.) Must Remember Source Type should be JSON and Annotation style should be GSON. Then at bottom press Preview. you will get all Model Classes. add those to your project
– Ali Ahmed
Nov 22 '18 at 9:59
Thanks, I will try :)
– Toni0123
Nov 22 '18 at 10:20
Tried, but it does not work. Call<TrackDetailsModel> call = IApiClient.getDatum(); needs to be static,
– Toni0123
Nov 22 '18 at 11:35
|
show 3 more comments
I am not sure, but adding my json does not provide me all that. api.deezer.com/chart
– Toni0123
Nov 22 '18 at 9:51
have you added JSON got fromapi.deezer.com/chart
tojsonScheme2pojo
?
– Ali Ahmed
Nov 22 '18 at 9:53
@Toni0123 when you open jsonschema2pojo there will beClass name
on Right Top, Change that from Example to TrackDetailsModel . Then add your all json (that you got fromapi.deezer.com/chart
to Text Section on Left.) Must Remember Source Type should be JSON and Annotation style should be GSON. Then at bottom press Preview. you will get all Model Classes. add those to your project
– Ali Ahmed
Nov 22 '18 at 9:59
Thanks, I will try :)
– Toni0123
Nov 22 '18 at 10:20
Tried, but it does not work. Call<TrackDetailsModel> call = IApiClient.getDatum(); needs to be static,
– Toni0123
Nov 22 '18 at 11:35
I am not sure, but adding my json does not provide me all that. api.deezer.com/chart
– Toni0123
Nov 22 '18 at 9:51
I am not sure, but adding my json does not provide me all that. api.deezer.com/chart
– Toni0123
Nov 22 '18 at 9:51
have you added JSON got from
api.deezer.com/chart
to jsonScheme2pojo
?– Ali Ahmed
Nov 22 '18 at 9:53
have you added JSON got from
api.deezer.com/chart
to jsonScheme2pojo
?– Ali Ahmed
Nov 22 '18 at 9:53
@Toni0123 when you open jsonschema2pojo there will be
Class name
on Right Top, Change that from Example to TrackDetailsModel . Then add your all json (that you got from api.deezer.com/chart
to Text Section on Left.) Must Remember Source Type should be JSON and Annotation style should be GSON. Then at bottom press Preview. you will get all Model Classes. add those to your project– Ali Ahmed
Nov 22 '18 at 9:59
@Toni0123 when you open jsonschema2pojo there will be
Class name
on Right Top, Change that from Example to TrackDetailsModel . Then add your all json (that you got from api.deezer.com/chart
to Text Section on Left.) Must Remember Source Type should be JSON and Annotation style should be GSON. Then at bottom press Preview. you will get all Model Classes. add those to your project– Ali Ahmed
Nov 22 '18 at 9:59
Thanks, I will try :)
– Toni0123
Nov 22 '18 at 10:20
Thanks, I will try :)
– Toni0123
Nov 22 '18 at 10:20
Tried, but it does not work. Call<TrackDetailsModel> call = IApiClient.getDatum(); needs to be static,
– Toni0123
Nov 22 '18 at 11:35
Tried, but it does not work. Call<TrackDetailsModel> call = IApiClient.getDatum(); needs to be static,
– Toni0123
Nov 22 '18 at 11:35
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427428%2fparsing-json-using-retrofit-gives-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
are you getting response null or some data null? Your interface seems ok if you are aspecting some response in DatumResponse format calling this url api.deezer.com/charts i thinks you are missing last "/" on BASE_URL
– justo
Nov 22 '18 at 9:21
1
change
https://api.deezer.com
tohttps://api.deezer.com/
– Ali Ahmed
Nov 22 '18 at 9:26
I am getting null, I need to get top 10 tracks from json.
– Toni0123
Nov 22 '18 at 9:26
@AliAhmed Added but still nothing
– Toni0123
Nov 22 '18 at 9:27
your JSON Response is not right. you should add all model classes and then get tracks out of them
– Ali Ahmed
Nov 22 '18 at 9:29