当我调用带有 openweathermap.com 空格的城市的 API 时,总是得到空响应

Always getting null response when i call an API with city with spaces at openweathermap.com

当我用带空格的城市调用 API 时,我得到了一个空响应:例如 "San Francisco"。例如,当 "Mexico" 工作正常时。

我试过用“+”替换空格,但它在 android 中不起作用。但是在邮递员中它起作用了。

这里是调用代码:

Api api = ApiBuilder.createApi();
    Call<CurrentWeather> currentWeatherCall = 
api.getCurrentWeather(API_KEY, "san+francisco", UNITS);

    currentWeatherCall.enqueue(new Callback<CurrentWeather>() {
        @Override
        public void onResponse(Call<CurrentWeather> call, 
Response<CurrentWeather> response) {
            try {
Toast.makeText(getApplicationContext(), response.body().getCityName(),
                        Toast.LENGTH_SHORT).show();
            }

如果您使用 logging-interceptor,那么您可以在详细日志中看到来自服务器的响应。

在Gradle implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'

在界面中

 public static PostService postService = null;

        public static PostService getService() {
        if (postService == null) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder()
           .addInterceptor(interceptor).build();

            Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(base_url)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
            postService = retrofit.create(PostService.class);

        }
        return postService;
        }

如果进行了 api 调用,那么服务器响应可以在 Verbose 中看到,然后你调试