I saw a lot of similar problems, but after many tries, still I don't know why my retrofit Get request returning null. I tried code this with many tutorials, spent one day on solution. URL from code should return all of cd's in database.
Below is client code in onCreate method in HomeFragment.class
Call<CD> call = RetrofitClient
.getInstance().getUserClient().getDataCD(token, "cds");
call.enqueue(new Callback<CD>() {
@Override
public void onResponse(Call<CD> call, Response<CD> response) {
if(!response.isSuccessful()){
System.out.println("Code: " + response.code());
return;
}else{
System.out.println(response.body());
}
}
@Override
public void onFailure(Call<CD> call, Throwable t) {
System.out.println(t.getMessage());
}
});
JSON model:
"cd": {
"artist": "string",
"cdtracks": [
{
"artist": "string",
"cd": [
null
],
"genre": "string",
"id": 0,
"releaseDate": 0,
"title": "string"
}
],
"comment": "string",
"comments": [
{
"content": "string",
"id": 0,
"user": {
"active": true,
"authToken": "string",
"badges": "string",
"email": "string",
"id": 0,
"isActive": true,
"nick": "string",
"photoURL": "string",
"registrationDate": 0,
"role": "string",
"score": 0,
"userscd": [
null
]
}
}
],
"genre": "string",
"id": 0,
"name": "string",
"photoURL": "string",
"rating": 0,
"released": 0,
"user": [
{
"active": true,
"authToken": "string",
"badges": "string",
"email": "string",
"id": 0,
"isActive": true,
"nick": "string",
"photoURL": "string",
"registrationDate": 0,
"role": "string",
"score": 0,
"userscd": [
null
]
}
]
}, }
CD.class
public class CD {
@SerializedName("id")
@Expose
private Long id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("released")
@Expose
private int released;
@SerializedName("rating")
@Expose
private float rating = 0;
@SerializedName("comment")
@Expose
private String comment;
private int ratingCount = 0;
private float sumOfRating = 0;
@SerializedName("photoURL")
@Expose
private String photoURL = "http://bobjames.com/wp-content/themes/soundcheck/images/default-album-artwork.png";
@SerializedName("artist")
@Expose
private String artist;
@SerializedName("cdtracks")
@Expose
private ArrayList<Track> cdtracks;
@SerializedName("genre")
@Expose
private String genre;
@SerializedName("user")
@Expose
private ArrayList<User> user;
@SerializedName("comments")
@Expose
private ArrayList<Comments> comments;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getReleased() {
return released;
}
public void setReleased(int released) {
this.released = released;
}
public float getRating() {
return rating;
}
public void setRating(float rating) {
this.rating = rating;
}
public String getArtist() {
return artist;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public void setArtist(String artist) {
this.artist = artist;
}
public ArrayList<Track> getCdtracks() {
return cdtracks;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public ArrayList<User> getUser() {
return user;
}
public void setUser(ArrayList<User> user) {
this.user = user;
}
public int getRatingCount() {
return ratingCount;
}
public void setRatingCount(int ratingCount) {
this.ratingCount = ratingCount;
}
public float getSumOfRating() {
return sumOfRating;
}
public void setSumOfRating(float sumOfRating) {
this.sumOfRating = sumOfRating;
}
public String getPhotoURL() { return photoURL; }
public void setPhotoURL(String photoURL) { this.photoURL = photoURL; }
public ArrayList<Comments> getComments() {
return comments;
}
public void setComments(ArrayList<Comments> comments) {
this.comments = comments;
}
@Override
public String toString() {
return "CD{" +
"id=" + id +
", name='" + name + '\'' +
", released=" + released +
", rating=" + rating +
", comment='" + comment + '\'' +
", ratingCount=" + ratingCount +
", sumOfRating=" + sumOfRating +
", photoURL='" + photoURL + '\'' +
", artist='" + artist + '\'' +
", cdtracks=" + cdtracks +
", genre='" + genre + '\'' +
", user=" + user +
", comments=" + comments +
'}';
}
}
I have similar built User.class, Artist.class, Track.class, Comment.class
My interface
public interface UserClient {
@POST("api/signup")
Call<User> createUser(
@Body User user
);
@Headers({ "Content-Type: application/json;charset=UTF-8"})
@POST("login")
Call<User> userLogin(
@Body Login login
);
@Headers({ "Content-Type: application/json;charset=UTF-8"})
@GET("api/{object}")
Call<CD> getDataCD(
@Header("Authorization") String authToken,
@Path("object") String object);
}
Instantiation of UserClient.class
public class RetrofitClient {
private static final String BASE_URL = "http://s416103.projektstudencki.pl:8080/";
private static RetrofitClient retrofitClient;
private Retrofit retrofit;
private Retrofit retrofitRegistration;
private RetrofitClient(){
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + token)
.build();
return chain.proceed(newRequest);
}
}).build();
retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
retrofitRegistration = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getInstance(){
if(retrofitClient == null){
retrofitClient = new RetrofitClient();
}
return retrofitClient;
}
public UserClient getUserClient(){
return retrofit.create(UserClient.class);
}
public UserClient getUserClientRegistration(){
return retrofitRegistration.create(UserClient.class);
}
}