0

I'm using retrofit 2 to make api call to my server but it get stucked when trying to make api call. This is my code

public interface GOTApi {


    @GET("characters.json")
    Call<GOTCharacterResponse> getCharacters();

}

Intermediate class to get the data

public class GOTCharacterResponse {

    List<GOTCharacter> characters;
}

My class to make api call

public class GOTService {

    public static final String BASE_URL = "https://project-8424324399725905479.firebaseio.com/";

    public static GOTApi getGOTApi(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                 .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit.create(GOTApi.class);

    }


    public static void getCharacters(){

        getGOTApi().getCharacters().enqueue(new Callback<GOTCharacterResponse>() {
            @Override
            public void onResponse(Call<GOTCharacterResponse> call, Response<GOTCharacterResponse> response) {

                if(response.isSuccessful()){

                }

            }

            @Override
            public void onFailure(Call<GOTCharacterResponse> call, Throwable t) {


                int a = 0;
            }
        });

    }

    }

These are the libraries I'm using

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp3:okhttp:3.3.1'

It always get stucked in the getCharacters() method. Of course I have internet permission set in Mainfest.

aloj
  • 3,194
  • 7
  • 27
  • 38

2 Answers2

0

You may try using Retrofit2 with RxJava, it is more convenient.

public Retrofit providedRetrofit(OkHttpClient okHttpClient){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BuildConfig.BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    return retrofit;
}

Your API interface will look like

public interface Api {
    @GET("api/service/schedule/{filial}")
    Observable<Response<GOTCharacter>>   getSchedule(@Path("some_param")  String param);
}

You also need to parse response from JSON. You didn't provided GOTCharacter class, but you can create code from json response by using http://www.jsonschema2pojo.org/ service

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11
  • RxJava might indeed be convenient in some cases, but I can't see how adding RxJava support will save OP's problem. – Egor Jun 23 '16 at 10:37
0

I think you are implementing wrong onResponse() OR Callback(), because I am using Retrofit 2 too, in which onResponse() looks like this:

@Override
public void onResponse(Response<ListJsonResponseRestaurant> response, Retrofit retrofit) {
    ...
    ...
}
Sajal Narang
  • 397
  • 1
  • 14