简体   繁体   English

Retrofit android - 无响应且无错误

[英]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.它通过callAsync.enqueue(new Callback<GetCheapestResponseType>()方法而不进入它。

This is my Service.class :这是我的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:这是我的Controller接口:

public interface Controller {

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

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.我过去遇到过这个问题,这个问题通常出现在您的 retrofit 客户端无法将您的响应解析为您的 Java class 时。 I suggest you to double check your output with your java class field.我建议您使用 java class 字段仔细检查您的 output。

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 "/".问题是@GET以“/”开头,而baseUrl有一个尾随“/”。

I had same problem, and checked my return type of Callback and response, then edited.我有同样的问题,并检查了我的回调和响应的返回类型,然后进行了编辑。 So it worked for me.所以它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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