我一直在使用Android做本教程。
我遇到的问题是,本教程是用更老的gson库和1.8.0改造库完成的。
我一直跟得很好,直到我遇到这个似乎无法解决的错误。

它与这一行有关.(这一行在我的MainActivity.Java中onCreate()下)
SCService scService = SoundCloud.getService();
scService.getRecentTracks(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()), new Callback<List<Track>>() {
@Override
public void onResponse(Response<List<Track>> tracks) {
Log.d("TAG", "ONRESPONSE() - -- - some else wrong");
// response.isSuccess() is true if the response code is 2xx
if (tracks.isSuccess()) {
Log.d("TAG", "ONRESPONSE()_isSuccess? - -- - some else wrong");
List<Track> track = tracks.body();
loadTracks(track);
} else {
Log.d("TAG", "some else wrong");
}
}
@Override
public void onFailure(Throwable t) {
// handle execution failures like no internet connectivity
Log.d("Error", t.getMessage());
}
});所以我认为问题是从scService接口开始的。
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.GET;
import retrofit2.http.Query;
interface SCService {
@GET("tracks?client_id=" + Config.CLIENT_ID)
public void getRecentTracks(@Query("created_at[from]") String date, Callback<List<Track>> cb);
}这是我的音云课..。
import retrofit2.Retrofit;
import retrofit2.Retrofit;
public class SoundCloud {
private static final Retrofit REST_ADAPTER = new Retrofit.Builder().baseUrl(Config.API_URL).build();
private static final SCService SERVICE = REST_ADAPTER.create(SCService.class);
public static SCService getService() {
return SERVICE;
}}
这是Config类认为不需要的.
public class Config {
public static final String CLIENT_ID = "c85f6828ae5eaf5981937ead09ef1b45";
public static final String API_URL = "https://api.soundcloud.com/";
}我一整天都在做这件事,任何帮助都是非常感谢的。
发布于 2016-01-28 15:56:27
这可能是少数事情,但最有可能的问题是,Gson转换器不再自动注册的改造,因此,您的代码不知道如何从Json获取对象。也就是说,在retrofit2中,您需要注册Gson,例如:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.nuuneoi.com/base/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();看一看本文:修改2项更改,自定义Gson对象(在页面中间)
https://stackoverflow.com/questions/34791973
复制相似问题