如何将Openweathermap图标代码转换为实际图像

时间:2018-11-27 17:12:01

标签: java android icons openweathermap

我需要帮助将OpenWeatherMap图标转换为实际的png图像。

到目前为止,我已将图标代码(iconName)检索到文本视图中,并为完整图像url(iconUrl)创建了String。

String iconName = JSONWeather.getString("icon");
String iconUrl = JSONWeather.getString(IMG_URL + iconName +".png");

但是我很难将此URL插入图像视图。

我收到的错误是

E /实用程序:解析地震JSON结果时出现问题     org.json.JSONException:https://openweathermap.org/img/w/04n.png

无值

我看到可以在这里使用毕加索,但是下面的代码为我显示了红色的viewHolder,我不知道需要添加什么来解决这个问题。

Picasso.with(viewHolder.itemView.getContext()).load(iconUrl).into(viewHolder.condIcon)



<ImageView
        android:id="@+id/condIcon"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
       />


   ImageView iconView =  findViewById(R.id.condIcon);
        iconView.setImageURI(Uri.parse(earthquake.iconUrl));

1 个答案:

答案 0 :(得分:0)

解决了! 值得一提的是,我的应用程序包含3个类:实用程序,MainActivity和事件。

首先,在Utils.java中解析JSON图标:

 /**URL for the icons location at OpenWeatherMap*/
    public static final String IMG_URL = "https://openweathermap.org/img/w/";

 //Retrieve icon code from website
                String iconName = JSONWeather.getString("icon");

第二,在事件中。 Java添加:

在顶部

public final String iconName;

和课程

 public Event(.....String eventIconName)
{ .......
     iconName=eventIconName;
      }

第三,在MainActivity.java中添加图像视图和毕加索参考

//Convert icon code from the OpenWeatherMap website into the image and insert into the ImageView. Path consists of URL declared in Utils.java, and @param iconName, declared in Event.java.
// JSON parser method used in Utils.java class retrieves icon code from OpenWeatherMap (iconName), .png extension is added.

    ImageView iconView =  findViewById(R.id.condIcon);
    Picasso.with(this).load(IMG_URL + weather.iconName +".png").into(iconView);

坦白说,我花了2天的时间弄清楚了这一点。希望这对其他人有帮助。