【问题标题】:How to convert Openweathermap icon code to actual image如何将 Openweathermap 图标代码转换为实际图像
【发布时间】:2018-11-27 17:12:01
【问题描述】:

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

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

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

但我很难将此网址插入图像视图。

我收到的错误是

E/Utils:解析地震 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));

【问题讨论】:

    标签: java android icons openweathermap


    【解决方案1】:

    解决了! 值得一提的是,我的应用包含 3 个类:Utils、MainActivity 和 Event。

    首先,在 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 天时间解决这个问题。希望这对其他人有所帮助。

    【讨论】:

    • 现在我有新问题 - 如何增加图标大小:P
    猜你喜欢
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2014-03-08
    • 2020-07-22
    • 2018-09-22
    • 2014-03-04
    相关资源
    最近更新 更多