You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法使用Picasso加载OpenWeather图标,请求排查问题原因

天气图标无法显示的核心原因排查与解决

结合你提供的代码,以下是导致OpenWeather API图标无法加载的关键问题及对应解决方案:

1. 明文HTTP请求被系统拦截(最常见)

Android 9(API 28)及以上版本默认禁止明文HTTP流量,你代码中使用的http://openweathermap.org/img/wn/04d@2x.png属于HTTP链接,会被系统直接拦截。

  • 解决方式:
    • 优先将链接替换为HTTPS协议:https://openweathermap.org/img/wn/04d@2x.png
    • 若必须使用HTTP,在AndroidManifest.xml的application标签内添加配置:
      <application
          ...
          android:usesCleartextTraffic="true">
          ...
      </application>
      

2. 缺少网络权限

若应用未声明网络权限,Picasso无法发起网络请求获取图片:

  • 检查AndroidManifest.xml是否添加权限声明:
    <uses-permission android:name="android.permission.INTERNET" />
    

3. Picasso依赖未正确引入

如果项目中未添加Picasso的依赖,代码可编译但运行时无法执行图片加载逻辑:

  • 在模块级build.gradle中添加依赖:
    implementation 'com.squareup.picasso:picasso:2.71828'
    

4. ImageView控件未初始化

代码中直接使用otherImg变量,但未看到通过findViewById初始化该控件的代码,若变量为空,Picasso无法将图片加载到目标控件:

  • 补充初始化代码:
    val otherImg = findViewById<ImageView>(R.id.your_image_view_id)
    
    注意替换your_image_view_id为布局文件中对应ImageView的实际ID。

5. 加载失败无反馈(排查辅助)

Picasso默认不会抛出加载异常,可添加回调监听定位具体错误:

Picasso.get()
    .load("https://openweathermap.org/img/wn/04d@2x.png")
    .error(R.drawable.error_placeholder) // 加载失败时显示本地占位图
    .into(otherImg, object : Callback {
        override fun onSuccess() {
            // 可选:打印加载成功日志
        }

        override fun onError(e: Exception?) {
            e?.printStackTrace() // 打印错误信息,快速定位问题
        }
    })

内容的提问来源于stack exchange,提问作者Anurag Hale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:18:26