改装android-无响应,无错误
我发送请求,但没有响应,也没有错误。它传递改装android-无响应,无错误,android,retrofit,Android,Retrofit,我发送请求,但没有响应,也没有错误。它传递callAsync.enqueue(newcallback()方法,而不输入它 这是我的服务。课堂: public class Service { String urlGetCheapest = "https://services-api.ryanair.com/farfnd/3/"; public void getCheapestFromToSpain() { OkHttpClient.Builder httpCl
callAsync.enqueue(newcallback()
方法,而不输入它
这是我的服务。课堂:
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);
}
});
}
}
公共类服务{
字符串urlGetCheapest=”https://services-api.ryanair.com/farfnd/3/";
public void getCheapestFromToSpain(){
OkHttpClient.Builder httpClient=新建OkHttpClient.Builder();
改装改装=新改装.Builder()
.baseUrl(urlGetCheapest)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
httpClient.connectTimeout(5,TimeUnit.MINUTES)//连接超时
.writeTimeout(5,TimeUnit.MINUTES)//写入超时
.readTimeout(5,TimeUnit.MINUTES);//读取超时
字符串outboundDepartureDateToString=“2021-12-31”;
Date outboundDepartureDateFrom=新日期(System.currentTimeMillis());
SimpleDataFormat sdf=新SimpleDataFormat(“yyyy-MM-dd”);
字符串outboundDepartureDateFromString=sdf.format(outboundDepartureDateFrom);
控制器=改装.create(Controller.class);
调用callAsync=controller.getCheapest(“SXF”,outboundDepartureDateFromString,outboundDepartureDateToString);
enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
GetCheapestResponseType apiResponse=response.body();
//API响应
系统输出打印项次(apiResponse);
}否则{
System.out.println(“请求错误:+response.errorBody());
}
}
@凌驾
失败时公共无效(调用、可丢弃){
System.out.println(可丢弃);
}
});
}
}
这是我的控制器界面:
public interface Controller {
@GET("/oneWayFares")
public Call<GetCheapestResponseType> getCheapest(
@Query("departureAirportIataCode") String departureAirportIataCode,
@Query("outboundDepartureDateFrom") String outboundDepartureDateFrom,
@Query("outboundDepartureDateTo") String outboundDepartureDateTo);
}
公共接口控制器{
@获取(“/单程票”)
公共电话最便宜(
@查询(“departureAirPortataCode”)字符串departureAirPortataCode,
@查询(“outboundDepartureDateFrom”)字符串outboundDepartureDateFrom,
@查询(“outboundDepartureDateTo”)字符串outboundDepartureDateTo);
}
我在过去遇到过这个问题,这个问题通常出现在改装客户端无法将响应解析到Java类中时。我建议您使用Java类字段仔细检查输出。我将答案放在这里,以便更直观地看到
问题是在
@GET
中有开头“/”,而baseUrl
有尾随“/”.我也遇到了同样的问题,检查了回调和响应的返回类型,然后进行了编辑。因此它对我很有效。您是否尝试过调试和检查正在发生的事情。查看日志也会很有帮助。仅通过检查url端点,我假设响应应该以数组开始,而不是以单个对象开始,所以可能调用但是可能会有更多的解析错误。我必须问一下,您是否已将
添加到清单中?@karan是的,我已对其进行调试。日志中没有任何内容,它正在传递callAsync.enqueue(new Callback()方法,而不输入它。您还应该删除“/”在您的@GET
中,因为您的baseUrlHmm中已经有尾随斜杠,所以没有输出。没有响应,也没有超时异常。