簡體   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