如何从OpenWeatherMap获取图标

时间:2017-08-14 11:52:49

标签: java android imageview openweathermap

我想获得openweathermap图标。 https://openweathermap.org/weather-conditionshttp://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1图标是字符串值。我创建了imageView,但我不知道

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int position, long id) {
            String text = (String) listView.getItemAtPosition(position);



            Factory.getInstance().havaModel(text,APPID_KEY).enqueue(new Callback<HavaModel>() {
                @Override
                public void onResponse(Call<HavaModel> call, Response<HavaModel> response) {
                    textView.setText(Float.toString((float) (response.body().main.temp-273.15)));
                    textView2.setText(Float.toString(response.body().coord.lon));
                    textView3.setText(Integer.toString(response.body().main.humidity));
                    textView4.setText(response.body().name);
                    imageView.getResources()



                }

enter image description here

非常感谢你。 你的建议对我很重要

2 个答案:

答案 0 :(得分:1)

图标的文档在这里。

http://openweathermap.org/weather-conditions

您将从JSON调用返回的对象中获取图标代码, 如您的问题所示,然后使用它来构建指向图标的网址

String iconUrl = "http://openweathermap.org/img/w/" + iconCode + ".png";

您还可以使用图像加载库将图像加载到ImageView中 - 例如: - 使用Picasso Image Library -

在build.gradle文件中添加它 -

compile 'com.squareup.picasso:picasso:2.5.2'

然后使用下面的代码在ImageView中加载图像 -

Picasso.with(context).load(iconUrl).into(imageView);

希望这有帮助!

答案 1 :(得分:0)

{
    "coord": {
        "lon": -0.13,
        "lat": 51.51
    },
    "weather": [
        {
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        }
    ],

从上面的json中,您可以获取图标ID并使用以下网址加载图片 http://openweathermap.org/img/w/10n.png

指向官方文档的链接是here