【问题标题】:Retrofit 2 GET request without endpoints?改造 2 GET 请求没有端点?
【发布时间】:2018-04-04 08:56:07
【问题描述】:

我想发送一个没有端点的“GET”请求,只是整个 url,但是当我发送我的请求时它不起作用,我只是从我的“onFailure”方法中得到答案。

这是我的网址: http://api.gios.gov.pl/pjp-api/rest/station/findAll(我已经在 Postman 上证明了这个网址,我可以在那里得到正确的回复)

这是我的改造客户:

     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 中的动态 url,但我不能正确地做到这一点。

【问题讨论】:

  • 你能把错误贴出来吗...?
  • 没有错误,我的意思是“public void onFailure”方法正在引起我的反应。我不知道我有哪个 response.code()
  • @Adam 在 onFailure() 中,查看错误添加 Log.e(TAG, t.toString());
  • LOG FROM ONFAILURE - com.google.gson.stream.MalformedJsonException:使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径 $ 接受格式错误的 JSON
  • 你从这个 Exception 中明白了什么,也许我不应该使用 Retrofit?

标签: java android retrofit2


【解决方案1】:

用途:

@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());
            }
        });

【讨论】:

  • 我正在尝试这个,但它没有帮助。我已经提出了很多要求,但这一个我找不到。
  • @Adam 我的意思是为什么不直接使用 @GET("findAll") Call showTheStations();
  • 我已经尝试过这个解决方案,但它不起作用我在 onFailure 方法中遇到了这个异常 "com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 列 1 路径 $"
  • @Adam 查看我更新的答案。它会起作用,我自己尝试过
  • 是的,它有效!惊人的。感谢您牺牲了您的时间:)
猜你喜欢
  • 1970-01-01
  • 2016-10-19
  • 2018-06-05
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
相关资源
最近更新 更多