1

我一直在使用 Android Studio IDE完成本教程。

我遇到的问题是本教程是使用较旧的 gson 库和改造 1.8.0 完成的......

我一直很好地使用retrofit2.0-beta3,直到我遇到了这个我似乎无法解决的错误..

在此处输入图像描述

它与这一行有关......(这一行在我的 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);
     }

这是我的 Soundcloud 课程....

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/";
}

我整天都在这,任何帮助将不胜感激..


4

1 回答 1

0

这可能是几件事,但最有可能的问题是 Gson 转换器不再自动注册改造,因此您的代码不知道如何从 Json 获取对象。即在r​​etrofit2中,您需要像这样注册Gson:

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.nuuneoi.com/base/")
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

看看这篇文章:Retrofit 2 changes, Custom Gson Object(在页面中间)

于 2016-01-28T15:56:27.520 回答