我看到了很多类似的问题,但是经过多次尝试,我仍然不知道为什么我的改造Get请求返回空值。我尝试了很多教程来编写代码,花了一天的时间在解决方案上。代码中的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());
}
});
JSON模型:
"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 +
'}';
}
}
我有类似的内置User.class,Artist.class,Track.class,Comment.class
我的界面
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);
}
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);
}
}
答案 0 :(得分:0)
您的翻新版Get请求返回null,这可能是因为您的Class CD与JSON模型不正确匹配。
您应该从api返回,只是属性“ cd”内的对象。
固定的JSON模型:
{
"genre": "string",
"id": 0,
"name": "string",
"photoURL": "string",
"rating": 0,
"released": 0,
(...)
}