简体   繁体   English

无法使用毕加索加载 openweatherIcon

[英]Not able to load openweatherIcon using Picasso

I am trying to use the openweather API for my app, and when I try to load the icons, I am not able to do so for some reasons.我正在尝试为我的应用程序使用 openweather API,当我尝试加载图标时,由于某些原因我无法这样做。

This is my mainActivity code that I'm trying to implement so that later on I can try it where I really require it.这是我正在尝试实现的 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)
    }

Any reasons as to why the image isn't being displayed at all.关于为什么根本不显示图像的任何原因。

I am using Glide and I add a callback to see what is the exception我正在使用 Glide 并添加一个回调以查看异常情况

the stacktrace print Caused by: java.io.IOException: Cleartext HTTP traffic to openweathermap.org not permitted .堆栈跟踪打印Caused by: java.io.IOException: Cleartext HTTP traffic to openweathermap.org not permitted So I added android:usesCleartextTraffic="true" to application in AndroidManifest and it works now.所以我在 AndroidManifest 中的应用程序中添加了android:usesCleartextTraffic="true" ,它现在可以工作了。

Try adding size for an image you are trying to display.尝试为您尝试显示的图像添加大小。 Like this:像这样:

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

Open weathermap supports https .打开天气图支持https So it's recommended to use https in url.所以推荐在url中使用https。 From android pie, http urls not permitted directly.从 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 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