Android-Openweathermap:搜索后的城市列表

时间:2018-08-08 05:39:59

标签: android retrofit retrofit2 openweathermap

我刚接触Android开发,并且正在尝试制作一个气象应用程序,该应用程序在搜索后会显示该城市的国家列表(例如伦敦)。在openweathermap api中,它显示了5个不同国家/地区的搜索结果,并在其上标有“ London”,但在我的代码中甚至没有显示1个结果。

这是我的代码:

public void getCityData(){
    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    WeatherService weatherService = retrofit.create(WeatherService.class);

    ListService listService = retrofit.create(ListService.class);


    String cityName = search.getEditableText().toString();

    stringArrayList.clear();
    stringArrayList.add(cityName);

    for (String city : stringArrayList) {
        Call<WeatherData> weatherDataCall = weatherService.getWeatherData("/data/2.5/find?q=" + city + "&APPID=" + API_KEY + "");
        weatherDataCall.enqueue(new Callback<WeatherData>() {
            @Override
            public void onResponse(Call<WeatherData> call, Response<WeatherData> response) {
                ProgressBar progressBar = new ProgressBar(AddCityActivity.this, null, android.R.attr.progressBarStyleHorizontal);
                progressBar.setIndeterminate(true);

                WeatherData weatherData = response.body();
                List<cp2.openweatherv3.model.List> lists = weatherData.getList();

                for (int i = 0; i < lists.size(); i++){
                    Log.d("name", lists.get(i).getName());
                    Log.e("country", lists.get(i).getSys().getCountry());
                    Log.w("count", String.valueOf(lists.size()));
                    generateList(lists);
                }

            }

            @Override
            public void onFailure(Call<WeatherData> call, Throwable t) {
                Log.d("sometag", t.getLocalizedMessage());
                Toast.makeText(AddCityActivity.this, "not throwing anything", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

如何使之成为可能?

非常感谢您

0 个答案:

没有答案