Json 弹簧&x2B;Android(2)获取请求返回null

Json 弹簧&x2B;Android(2)获取请求返回null,json,spring,get,null,retrofit2,Json,Spring,Get,Null,Retrofit2,我看到了很多类似的问题,但是经过多次尝试,我仍然不知道为什么我的改装Get请求返回null。我用很多教程编写了这段代码,花了一天时间编写了这个解决方案。代码中的URL应返回数据库中的所有cd 下面是HomeFragment.class中onCreate方法中的客户端代码 Call<CD> call = RetrofitClient .getInstance().getUserClient().getDataCD(token, "cds"); cal

我看到了很多类似的问题,但是经过多次尝试,我仍然不知道为什么我的改装Get请求返回null。我用很多教程编写了这段代码,花了一天时间编写了这个解决方案。代码中的URL应返回数据库中的所有cd

下面是HomeFragment.class中onCreate方法中的客户端代码

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

光盘类

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

}

您的改装Get请求返回null,可能是因为您的类CD与JSON模型不正确匹配

您应该从api返回,只返回属性“cd”中的对象

JSON模型已修复:

{
  "genre": "string",
  "id": 0,
  "name": "string",
  "photoURL": "string",
  "rating": 0,
  "released": 0,
  (...)
}

关于问题,请添加您的UserClient实例化代码。我编辑了问题并添加了实例化代码。我的建议是调试对API的调用。使用此:。使用它,您可以在Logcat上看到来自api的数据!这只适用于一张CD。如果您有兴趣从api返回CD列表,我可以帮助您。我非常感谢您的帮助。首先我编写调试调用的代码,然后我看到了我的错误。
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);
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);
}
{
  "genre": "string",
  "id": 0,
  "name": "string",
  "photoURL": "string",
  "rating": 0,
  "released": 0,
  (...)
}