简体   繁体   中英

Fatal exception. Using Retrofit2-beta3 cannot retrieve data from apiUrl using retrofit2

Ive been doing this tutorial using Android Studio IDE.

The problem I have is that the tutorial was done with older libraries of gson and retrofit 1.8.0...

I was following along well with retrofit2.0-beta3 until I came upon this error that I cant seem to resolve..

在此处输入图片说明

It has something to do with this line...(this line is in my MainActivity.Java under 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());
       }
   });

so I think that the problem starts with scService Interface..

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);
     }

Here is my Soundcloud class....

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

}

This is the Config class didnt think it would be needed...

public class Config {

public static final String CLIENT_ID = "c85f6828ae5eaf5981937ead09ef1b45";
public static final String API_URL = "https://api.soundcloud.com/";
}

I have been at this the whole day, Any help would be much appreciated..


It could be few things, but most likely the problem is that Gson converter is no longer automatically registered with retrofit and thus your code doesn't know how to get you object from Json. Ie in retrofit2 you need to register Gson like:

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

Take a look at this article: Retrofit 2 changes, Custom Gson Object (in the middle of the page)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM