繁体   English   中英

无法使用毕加索加载 openweatherIcon

[英]Not able to load openweatherIcon using Picasso

我正在尝试为我的应用程序使用 openweather API,当我尝试加载图标时,由于某些原因我无法这样做。

这是我正在尝试实现的 mainActivity 代码,以便稍后我可以在我真正需要的地方尝试它。

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val layout = findViewById<ConstraintLayout>(R.id.Rlayout)
        val eTime = currentTimeMillis()
        val format = SimpleDateFormat("HH", Locale.getDefault())
        val time = format.format(eTime).toInt()

        if(time in 6..11) {
            layout.setBackgroundResource(R.drawable.gradientmorning)
        } else if (time in 12..18) {
            layout.setBackgroundResource(R.drawable.gradientnoon)
        } else if (time > 18 || time < 6) {
            layout.setBackgroundResource(R.drawable.gradientnight)
        }

        context = applicationContext
        mRequest = Singleton.getInstance(this)
        progressBar = findViewById(R.id.loader)

        dailyRV.layoutManager = LinearLayoutManager(this)                      // dailyForecastAdapter
        dailyAdapter = DailyInfoAdapter()
        dailyRV.adapter = dailyAdapter

        hourlyRV.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)       //hourlyAdapter
        hourlyAdapter = HourlyInfoAdapter()
        hourlyRV.adapter = hourlyAdapter

        locationVariable = LocationServices.getFusedLocationProviderClient(this)
        getCLocation(context,mRequest)
        progressBar.visibility = View.VISIBLE
        
        Picasso.get().load("http://openweathermap.org/img/wn/04d@2x.png").into(otherImg)
    }

关于为什么根本不显示图像的任何原因。

我正在使用 Glide 并添加一个回调以查看异常情况

堆栈跟踪打印Caused by: java.io.IOException: Cleartext HTTP traffic to openweathermap.org not permitted 所以我在 AndroidManifest 中的应用程序中添加了android:usesCleartextTraffic="true" ,它现在可以工作了。

尝试为您尝试显示的图像添加大小。 像这样:

Picasso.get()
            .load("http://openweathermap.org/img/wn/04d@2x.png")
            .resize(255,255)
            .centerCrop()
            .into(otherImg);

打开天气图支持https 所以推荐在url中使用https。 从 android 派,http url 不允许直接。 You have to use https or if https is not supported, you should add cleartexttraffic flag or xml network config for particular domain in AndroidManifest.xml

Picasso.get().load("https://openweathermap.org/img/wn/04d@2x.png").into(otherImg)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM