Java 改型2 Android:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$

Java 改型2 Android:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$,java,android,json,retrofit,retrofit2,Java,Android,Json,Retrofit,Retrofit2,我知道这不是第一次有人问这个问题,但是改型2后我找不到解决问题的正确方法。我学习了一个在线教程,效果很好。当我将相同的代码应用到我自己的端点时,我得到了这个异常:java.lang.IllegalStateException:应该是BEGIN\u数组,但在第1行第2列路径$处是BEGIN\u对象,我不知道如何解决这个问题 接口: public interface MyApiService { // Is this right place to add these headers? @Heade

我知道这不是第一次有人问这个问题,但是改型2后我找不到解决问题的正确方法。我学习了一个在线教程,效果很好。当我将相同的代码应用到我自己的端点时,我得到了这个异常:
java.lang.IllegalStateException:应该是BEGIN\u数组,但在第1行第2列路径$
处是BEGIN\u对象,我不知道如何解决这个问题

接口:

public interface MyApiService {

// Is this right place to add these headers?
@Headers({"application-id: MY-APPLICATION-ID",
        "secret-key: MY-SECRET-KEY",
        "application-type: REST"})
@GET("Music")
Call<List<Music>> getMusicList();



Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(MySettings.REST_END_POINT)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
}
但这一次不行:

{
"offset": 0,
"data": [
    {
        "filename": "E743_1458662837071.mp3",
        "created": 1458662854000,
        "publicUrl": "https://api.backendless.com/dbb77803-1ab8-b994-ffd8-65470fa62b00/v1/files/music/E743_1458662837071.mp3",
        "___class": "Music",
        "description": "",
        "likeCount": 0,
        "title": "hej Susanne. ",
        "ownerId": "E743756F-E114-6892-FFE9-BCC8C072E800",
        "updated": null,
        "objectId": "DDD8CB3D-ED66-0D6F-FFA5-B14543ABC800",
        "__meta": "{\"relationRemovalIds\":{},\"selectedProperties\":[\"filename\",\"created\",\"publicUrl\",\"___class\",\"description\",\"likeCount\",\"title\",\"ownerId\",\"updated\",\"objectId\"],\"relatedObjects\":{}}"
    },
    {...
我的音乐课:

public class Music {

   private String ownerId;
   private String filename;
   private String title;
   private String description;
   private String publicUrl;
   private int likeCount;

   // Getters & Setters

}
当您说“此代码正在使用此有效负载:……但不使用此有效负载:……”时,这是预期的结果,这就是它的工作方式。事实上,错误消息告诉您,在将json转换为java对象时,调用期望json中有一个数组,但得到了一个对象

此电话:

@GET("Music")
Call<List<Music>> getMusicList();
因为json本身是
Music
对象的数组(改型可以将json数组转换为java列表)。对于第二个json,您只有一个对象而没有数组(请注意缺少
[…]
)。为此,您需要使用映射到该json的另一个模型创建另一个调用。假设您已将模型命名为
MusicList
。下面是电话的样子:

@GET("Music")
Call<MusicList> getMusicList();

我假设
数据
数组是
音乐
对象的列表,但我注意到JSON是完全不同的。您可能也需要对此进行调整,但我认为您已经了解了情况。

我遇到了这个问题。因为playload是对象而不是对象数组。所以我删除了这个列表

代码示例

UserAPI.java

public interface UserAPI {
    @GET("login/cellphone")
    Call<LoginResponse> login(@Query("phone") String phone, 
                              @Query("password") String password);
}
公共接口UserAPI{
@获取(“登录/手机”)
呼叫登录(@Query(“电话”)字符串电话,
@查询(“密码”)字符串(密码);
}
呼叫码

Retrofit retrofit = new Retrofit
        .Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(Constant.CLOUD_MUSIC_API_BASE_URL)
        .build();

UserAPI userAPI = retrofit.create(UserAPI.class);

userAPI.login(phone, password).enqueue(new Callback<LoginResponse>() {
    @Override
    public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
        System.out.println("onResponse");
        System.out.println(response.body().toString());
    }

    @Override
    public void onFailure(Call<LoginResponse> call, Throwable t) {
        System.out.println("onFailure");
        System.out.println(t.fillInStackTrace());
    }
});
改装改装=新改装
.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(常量.CLOUD\u MUSIC\u API\u BASE\u URL)
.build();
UserAPI UserAPI=reformation.create(UserAPI.class);
userAPI.login(电话、密码).enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
System.out.println(“onResponse”);
System.out.println(response.body().toString());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
System.out.println(“onFailure”);
System.out.println(t.fillInStackTrace());
}
});

看一看。您需要返回一个不同的类,该类包含一个
列表数据
它需要一个数组,您给它一个objectA side注意:使用GET作为登录方法存在一些安全问题,您应该选择POST。请检查这个,非常感谢你分享这个,我花了一整天的时间试图找到一个解决方案,感谢你的提示,它成功了。非常感谢你!!
@GET("Music")
Call<MusicList> getMusicList();
public class MusicList {
  @SerializedName("data")
  private List<Music> musics;
  // ...
}
public interface UserAPI {
    @GET("login/cellphone")
    Call<LoginResponse> login(@Query("phone") String phone, 
                              @Query("password") String password);
}
Retrofit retrofit = new Retrofit
        .Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(Constant.CLOUD_MUSIC_API_BASE_URL)
        .build();

UserAPI userAPI = retrofit.create(UserAPI.class);

userAPI.login(phone, password).enqueue(new Callback<LoginResponse>() {
    @Override
    public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
        System.out.println("onResponse");
        System.out.println(response.body().toString());
    }

    @Override
    public void onFailure(Call<LoginResponse> call, Throwable t) {
        System.out.println("onFailure");
        System.out.println(t.fillInStackTrace());
    }
});