簡體   English   中英

Retrofit android - 無響應且無錯誤

[英]Retrofit android - no response and no error

我發送請求,但沒有響應,也沒有錯誤。 它通過callAsync.enqueue(new Callback<GetCheapestResponseType>()方法而不進入它。

這是我的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);
            }
        });
    }
}

這是我的Controller接口:

public interface Controller {

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

我過去遇到過這個問題,這個問題通常出現在您的 retrofit 客戶端無法將您的響應解析為您的 Java class 時。 我建議您使用 java class 字段仔細檢查您的 output。

我把答案放在這里,所以它更明顯。

問題是@GET以“/”開頭,而baseUrl有一個尾隨“/”。

我有同樣的問題,並檢查了我的回調和響應的返回類型,然后進行了編輯。 所以它對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM