繁体   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