janusz j
janusz j

Reputation: 329

Retrofit android - no response and no error

I send request but there is no response and no error. It passes callAsync.enqueue(new Callback<GetCheapestResponseType>() method without entering into it.

This is my Service.class:

public class Service {

    String urlGetCheapest = "https://services-api.ryanair.com/farfnd/3/";

    public void getCheapestFromToSpain() {

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(urlGetCheapest)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();

        httpClient.connectTimeout(5, TimeUnit.MINUTES) // connect timeout
                .writeTimeout(5, TimeUnit.MINUTES) // write timeout
                .readTimeout(5, TimeUnit.MINUTES); // read timeout

        String outboundDepartureDateToString = "2021-12-31";

        Date outboundDepartureDateFrom = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String outboundDepartureDateFromString = sdf.format(outboundDepartureDateFrom);

        Controller controller = retrofit.create(Controller.class);

        Call<GetCheapestResponseType> callAsync = controller.getCheapest("SXF", outboundDepartureDateFromString, outboundDepartureDateToString);

        callAsync.enqueue(new Callback<GetCheapestResponseType>() {
            @Override
            public void onResponse(Call<GetCheapestResponseType> call, Response<GetCheapestResponseType> response) {
                if (response.isSuccessful()) {
                    GetCheapestResponseType apiResponse = response.body();

                    //API response
                    System.out.println(apiResponse);
                } else {
                    System.out.println("Request Error : " + response.errorBody());
                }
            }

            @Override
            public void onFailure(Call<GetCheapestResponseType> call, Throwable throwable) {
                System.out.println(throwable);
            }
        });
    }
}

This is my Controller interface:

public interface Controller {

    @GET("/oneWayFares")
    public Call<GetCheapestResponseType> getCheapest(
            @Query("departureAirportIataCode") String departureAirportIataCode,
            @Query("outboundDepartureDateFrom") String outboundDepartureDateFrom,
            @Query("outboundDepartureDateTo") String outboundDepartureDateTo);
}

Upvotes: 0

Views: 2566

Answers (3)

Sevket
Sevket

Reputation: 914

I had same problem, and checked my return type of Callback and response, then edited. So it worked for me.

Upvotes: 1

Biscuit
Biscuit

Reputation: 5247

I'm putting the answer here so it's more visible.

The issue was that in the @GET had beginning "/" while the baseUrl had a trailing "/".

Upvotes: 0

Parth
Parth

Reputation: 791

I've came across this problem in the past, This problem usually comes when your retrofit client can't parse your response into your Java class. I suggest you to double check your output with your java class field.

Upvotes: 2

Related Questions