【问题标题】:How do I properly set the annotation and query in Retrofit 2.0+?如何在 Retrofit 2.0+ 中正确设置注释和查询?
【发布时间】:2017-05-31 19:33:24
【问题描述】:
URL query string "q={city}" must not have replace block

我无法让它工作,我尝试了其他几种变体,但仍然出现某种形式的异常。

public interface WeatherInterface {

    @GET("/weather?q={city}")
    Call<WeatherModel> getWeather(@Query("city") String city);

}

/////

public interface WeatherInterface {

    @GET("/weather")
    Call<WeatherModel> getWeather(@Query("q") String city);

}

等等。

WeatherActivity.class

Call<WeatherModel> call = weatherInterface.getWeather("""CITYNAME""");
        call.enqueue(new Callback<WeatherModel>() {
            @Override
            public void onResponse(Call<WeatherModel> call, Response<WeatherModel> response) {
                if(response.isSuccessful()) {
                    **///FIRST VARIANT FAILS HERE**
                    city.setText(response.body().getName());
                }
                **///SECOND VARIANT FAILES RESPONSE**
                else Log.d("No response", "RESPONSE");
            }

            @Override
            public void onFailure(Call<WeatherModel> call, Throwable t) {
                Log.d("fail", "fail");
            }
        });

编辑: Log.d(call.request().url().toString(), "CALL REQUEST URL");

我也应该分享我的解决方案,我刚刚记录了通话网址。

【问题讨论】:

    标签: java android api retrofit


    【解决方案1】:

    我忘记在网址中添加我的 API KEY。我可耻地撤回了我的问题。

    【讨论】:

      【解决方案2】:

      你可以使用这个示例来举例:

         public interface GitHubClient {  
              @GET("/users/{user}/repos")
              Call<List<GitHubRepo>> reposForUser(
                  @Path("user") String user
              );
          }
      

      更多样品请访问website

      【讨论】:

      • 是的,我检查了文档,但显然我的 api 密钥丢失了。该日志还显示了 url 调用的样子,如果这对任何人都有帮助的话。
      猜你喜欢
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多