【问题标题】:How to pass some parameters to the @GET() method on Retrofit Java Interface?如何将一些参数传递给 Retrofit Java 接口上的 @GET() 方法?
【发布时间】:2016-08-05 08:38:43
【问题描述】:

我正在使用 wunderground API 制作天气应用程序。我也在使用 Retrofit2 和 GSON 库。

这是获取 JSON 响应的 API URL 格式:

http://api.wunderground.com/api/API_KEY/conditions/q/ISO_COUNTRY_CODE/CITY_NAME.json

我已经声明了一个 java API_Interface 如下:

public interface API_Interface {

    @GET("/api/{apikey}/conditions/q/BD/{city}.json")
    Call<CurrentObservation> getCurrentWeather(
            @Path("apikey") String apikey,
            @Path("city") String city);
}

并尝试从MainActivity 传递apikeycity,如下所示:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        API_Interface weatherService = retrofit.create(API_Interface.class);
        Call<CurrentObservation> call = weatherService.getCurrentWeather(Constants.API_KEY,"Dhaka");

        call.enqueue(new Callback<CurrentObservation>() {
            @Override
            public void onResponse(Call<CurrentObservation> call, Response<CurrentObservation> response) {
                textView.setText(response.body().toString());
                Log.d("result",response.body().toString());
            }

            @Override
            public void onFailure(Call<CurrentObservation> call, Throwable t) {
                textView.setText("Something went wrong: " + t.getMessage());
                Log.e("error",t.getMessage());
            }
        });

这是Constant 类:

public class Constants {
    public static final String BASE_URL="http://api.wunderground.com";
    public static final String API_KEY="b5efba6dc63cc1b1";
}

这是CurrentObservation类的POJO模型:http://paste.ubuntu.com/22291964/ 我已经覆盖了模型中的 toString() 方法。

还有一些其他的 POJO 类-

但是这种方法给出了如下的空响应-

Weather Status: null
Pressure: null
Humidity: null
Temperature: null

这是来自 API URL 的实际 JSON 响应 - http://paste.ubuntu.com/22292683/

如何将参数传递给@GET 以获得正确的响应?

【问题讨论】:

  • 这是正确的。你确定Constants.API_KEY 是正确的吗?响应的 HTTP 代码是什么?
  • 添加了更多详细信息。请立即查看。

标签: java android json gson retrofit


【解决方案1】:

您的基本 URL 应如下所示:

http://blah.com/api/blah/   

你的@GET 方法应该有这样的 URL

api/{apikey}/conditions/q/BD/{city}.json

编辑:您可能使用error body 调用了onResponse。请根据您的用例调整以下代码:

public static boolean handleError(Retrofit retrofit, Response<?> response) {
    if(response != null && !response.isSuccessful() && response.errorBody() != null) {
        Converter<ResponseBody, ErrorResponse> converter = retrofit.responseBodyConverter(ErrorResponse.class, new Annotation[0]);
        try {
            ErrorResponse errorResponse = converter.convert(response.errorBody());
            // do something
        } catch(IOException e) {
            Log.e(TAG, "An error occurred", e);
        }
        return true;
    }
    return false;
}

【讨论】:

  • 我按照您的建议编辑了代码,现在看起来像 - public static final String BASE_URL="api.wunderground.com" 和 @GET("api/{apikey}/conditions/q/BD/{city }.json") 但仍然是相同的结果。空响应。
  • 啊...这很奇怪。你可以试试这个:api/{apikey}/conditions/q/BD/{city}Call&lt;CurrentObservation&gt; call = weatherService.getCurrentWeather(Constants.API_KEY,"Dhaka.json");?只是为了消除可能导致错误的原因。
  • 试过..还是一样的结果:(。我已经添加了更多细节..你能再检查一次吗?
  • 好吧,你可能有一个成功但错误的主体,让我抓住代码来测试它
【解决方案2】:

你可以这样做:

@GET
Call<CurrentObservation> getCurrentWeather(@Url String url);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-19
    • 2020-09-11
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2018-01-27
    • 2016-10-22
    • 2013-10-04
    相关资源
    最近更新 更多