我想发送一个没有端点的“GET”请求只是整个网址,但是当我发送请求时它不起作用,我只是从我的“onFailure”方法中回答。
这是我的网址: http://api.gios.gov.pl/pjp-api/rest/station/findAll(我在Postman上证明了这个网址,我可以得到正确答复)
这是我的Retrofit客户:
private static final String BASE_URL = "http://api.gios.gov.pl/pjp-api/rest/station/findAll/";
private static Retrofit retrofit = null;
public static Retrofit getRetrofitClient(String url){
if(retrofit==null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
这是界面:
@GET()
Call<ServerResponse> showTheStations(@Url String url);
我在MainActivity中的响应方法:
public void retrofitRespond(){
retrofitInterface = ApiUtil.getRetrofitInterface();
retrofitInterface.showTheStations("http://api.gios.gov.pl/pjp-
api/rest/station/findAll").enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
Toast.makeText(getApplicationContext(),"WENT WRONG", Toast.LENGTH_LONG).show();
}
});
我该怎么做才能正确?我在Retrofit 2中已经阅读过有关动态网址的内容,但我无法正确完成。
答案 0 :(得分:4)
使用:
@GET()
Call<List<ServerResponse>> showTheStations(@Url String url);
在MainActivity中:
Call<List<ServerResponse>> call = retrofitInterface.showTheStations("http://api.gios.gov.pl/pjp-api/rest/station/findAll/");
call.enqueue(new Callback<List<ServerResponse>>() {
@Override
public void onResponse(Call<List<ServerResponse>> call, Response<List<ServerResponse>> response) {
Log.i(TAG, response.body().toString());
List<ServerResponse> body = response.body();
Log.i(TAG, body.get(0).getId()+"");
}
@Override
public void onFailure(Call<List<ServerResponse>> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});