Retrofit 和 Spotify API:预期 BEGIN_OBJECT 但在第 1 行第 1 列路径 $ 处为 STRING
Retrofit and Spotify API: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
Whosebug 上有几个问题和我的一样,但我一直没能解决我的问题。错误如题中所述。我正在与 Spotify API 合作以获得 current user's profile。我觉得我对正在发生的事情有根本的误解。
这是我的主要 activity:
Gson gson1 = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.setLenient()
.create();
Retrofit retrofit1 = new Retrofit.Builder()
.baseUrl(ENDPOINT_ACCOUNTS)
.addConverterFactory(GsonConverterFactory.create(gson1))
.build();
SpotifyAPI spotifyAPI = retrofit1.create(SpotifyAPI.class);
Call<User> call1 = spotifyAPI.getUserData("Authorization: Bearer " + sp.getString("accessCode", "DEFAULT"));
call1.enqueue(new Callback<User> () {
@Override
public void onResponse(Call<User> call, Response<User> response) {
...
}
@Override
public void onFailure(Call call, Throwable t) {
...
}
});
这是我的界面:
String ENDPOINT_V1 = "https://api.spotify.com/";
@GET("/v1/me")
Call<User> getUserData(@Header("Authorization") String authorization);
这是我的用户 class:
public class User {
String id;
public String toString() {
return(id);
}
}
OnFailure() 被触发,吐出了标题中的错误信息。如果我用 ResponseBody 替换 User 的所有实例,调用就会成功,状态代码为 200。但是响应的内容根本不是我想要的。如果我可以提供更多信息,请告诉我。谢谢。
原来我的基础 URL 错了。现在就像一个魅力。感谢您仔细阅读。
Whosebug 上有几个问题和我的一样,但我一直没能解决我的问题。错误如题中所述。我正在与 Spotify API 合作以获得 current user's profile。我觉得我对正在发生的事情有根本的误解。
这是我的主要 activity:
Gson gson1 = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.setLenient()
.create();
Retrofit retrofit1 = new Retrofit.Builder()
.baseUrl(ENDPOINT_ACCOUNTS)
.addConverterFactory(GsonConverterFactory.create(gson1))
.build();
SpotifyAPI spotifyAPI = retrofit1.create(SpotifyAPI.class);
Call<User> call1 = spotifyAPI.getUserData("Authorization: Bearer " + sp.getString("accessCode", "DEFAULT"));
call1.enqueue(new Callback<User> () {
@Override
public void onResponse(Call<User> call, Response<User> response) {
...
}
@Override
public void onFailure(Call call, Throwable t) {
...
}
});
这是我的界面:
String ENDPOINT_V1 = "https://api.spotify.com/";
@GET("/v1/me")
Call<User> getUserData(@Header("Authorization") String authorization);
这是我的用户 class:
public class User {
String id;
public String toString() {
return(id);
}
}
OnFailure() 被触发,吐出了标题中的错误信息。如果我用 ResponseBody 替换 User 的所有实例,调用就会成功,状态代码为 200。但是响应的内容根本不是我想要的。如果我可以提供更多信息,请告诉我。谢谢。
原来我的基础 URL 错了。现在就像一个魅力。感谢您仔细阅读。