【问题标题】:Fatal exception. Using Retrofit2-beta3 cannot retrieve data from apiUrl using retrofit2致命的例外。使用 Retrofit2-beta3 无法使用 retrofit2 从 apiUrl 检索数据
【发布时间】:2016-01-14 14:25:13
【问题描述】:

我一直在使用 Android Studio IDE 做this tutorial

我遇到的问题是本教程是使用较旧的 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/";
}

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


【问题讨论】:

  • 您还应该为 client_id 使用 @Query 并发布您的 Config.API_URL
  • 我已经发布了 Config.java 文件 - 它有 Config.API_URL ... 还有如何 @Query 获取 client_id?在接口类中?我不明白...
  • ?client_id=" + Config.CLIENT_ID 可以替换为@Query("client_id") String clientId

标签: java android soundcloud retrofit2


【解决方案1】:

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

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

看看这篇文章:Retrofit 2 changes, Custom Gson Object (in the middle of the page)

【讨论】:

    猜你喜欢
    • 2019-06-12
    • 1970-01-01
    • 2020-04-28
    • 2021-10-26
    • 2019-09-30
    • 2018-07-06
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    相关资源
    最近更新 更多