当JSON对象URL字段在android studio中包含多个URL时,如何存储来自JSON对象请求的URL?

Posted

技术标签:

【中文标题】当JSON对象URL字段在android studio中包含多个URL时,如何存储来自JSON对象请求的URL?【英文标题】:How to store URL from JSON object request when JSON object URL field contains multiple URL in android studio? 【发布时间】:2021-07-02 22:44:41 【问题描述】:

主活动文件代码- ...

    import android.content.Intent
    import android.content.Intent.createChooser
    import android.graphics.drawable.Drawable
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.util.Log
    import android.view.View
    import android.widget.Toast
    import com.android.volley.Request
    import com.android.volley.toolbox.JsonObjectRequest
    import com.bumptech.glide.Glide
    import com.bumptech.glide.load.DataSource
    import com.bumptech.glide.load.engine.GlideException
    import com.bumptech.glide.request.RequestListener
    import com.example.memesexplorer.databinding.ActivityMainBinding
    import com.bumptech.glide.request.target.Target
    
    class MainActivity : AppCompatActivity() 
        private var currentUrl: String? = null
        private lateinit var binding: ActivityMainBinding
    
        override fun onCreate(savedInstanceState: Bundle?) 
            super.onCreate(savedInstanceState)
            binding = ActivityMainBinding.inflate(layoutInflater)
            val view = binding.root
            setContentView(view)
    
            loadImage()
        
    
        private fun loadImage() 
            binding.next.isEnabled=false
            binding.share.isEnabled=false
            binding.buffer.visibility=View.VISIBLE
    
            val url="https://api.unsplash.com/photos/random?client_id=Q-fqoRMI-t8jb8se08Ow5hebv7KsDdWUBadjBwfqsxg"
            val jsonObjectRequest = JsonObjectRequest(
                Request.Method.GET, url,null,
                 response ->
                    Log.d("Response Check","Entered the response")
                    binding.buffer.visibility=View.GONE
                    currentUrl=response.getString("url")
                    Glide.with(this).load(currentUrl).listener(object : RequestListener<Drawable> 
                        override fun onResourceReady(
                            resource: Drawable?,
                            model: Any?,
                            target: Target<Drawable>?,
                            dataSource: DataSource?,
                            isFirstResource: Boolean
                        ): Boolean 
                            binding.buffer.visibility = View.GONE
                            binding.next.isEnabled = true
                            binding.share.isEnabled = true
                            return false
                        
    
                        override fun onLoadFailed(
                            e: GlideException?,
                            model: Any?,
                            target: Target<Drawable>?,
                            isFirstResource: Boolean
                        ): Boolean 
                            binding.buffer.visibility = View.GONE
                            binding.next.isEnabled = true
                            binding.share.isEnabled = true
                            Toast.makeText(this@MainActivity,"Image can't be loaded",Toast.LENGTH_LONG).show()
                            return false
                        
                    ).into(binding.img)
                
            ) 
                binding.buffer.visibility = View.GONE
                binding.next.isEnabled = true
                binding.share.isEnabled = true
                Toast.makeText(this, getString(R.string.error), Toast.LENGTH_LONG).show()
            
            Network.getInstance(this).addToRequestQueue(jsonObjectRequest)
        
    
    

...

我想改变 loadImage() 函数中 currentUrl 变量的赋值,以便它可以存储可以在 img ImageView 中加载的图像的 URL。

使用的API链接是-https://api.unsplash.com/photos/random?client_id=Q-fqoRMI-t8jb8se08Ow5hebv7KsDdWUBadjBwfqsxg

所用 API 的 JSON 对象是- ...

id: "JS2IVAyMsM0",
created_at: "2021-03-24T05:19:52-04:00",
updated_at: "2021-04-06T06:41:49-04:00",
promoted_at: "2021-03-24T10:21:01-04:00",
width: 3264,
height: 4896,
color: "#8cd9d9",
blur_hash: "L=F%rTt7oLoeyskCWCbHNxj[oLay",
description: null,
alt_description: "brown and white concrete buildings under blue sky during daytime",
urls: 
raw: "https://images.unsplash.com/photo-1616577441006-8a6b3fc50a7d?ixid=MnwyMjExMjV8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTc3NzU5MTE&ixlib=rb-1.2.1",
full: "https://images.unsplash.com/photo-1616577441006-8a6b3fc50a7d?crop=entropy&cs=srgb&fm=jpg&ixid=MnwyMjExMjV8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTc3NzU5MTE&ixlib=rb-1.2.1&q=85",
regular: "https://images.unsplash.com/photo-1616577441006-8a6b3fc50a7d?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMjExMjV8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTc3NzU5MTE&ixlib=rb-1.2.1&q=80&w=1080",
small: "https://images.unsplash.com/photo-1616577441006-8a6b3fc50a7d?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMjExMjV8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTc3NzU5MTE&ixlib=rb-1.2.1&q=80&w=400",
thumb: "https://images.unsplash.com/photo-1616577441006-8a6b3fc50a7d?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMjExMjV8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTc3NzU5MTE&ixlib=rb-1.2.1&q=80&w=200"
,
links: 
self: "https://api.unsplash.com/photos/JS2IVAyMsM0",
html: "https://unsplash.com/photos/JS2IVAyMsM0",
download: "https://unsplash.com/photos/JS2IVAyMsM0/download",
download_location: "https://api.unsplash.com/photos/JS2IVAyMsM0/download?ixid=MnwyMjExMjV8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MTc3NzU5MTE"
,
categories: [ ],
likes: 128,
liked_by_user: false,
current_user_collections: [ ],
sponsorship: null,
user: 
id: "VeIm9BP-du0",
updated_at: "2021-04-07T02:09:48-04:00",
username: "cristina_gottardi",
name: "Cristina Gottardi",
first_name: "Cristina",
last_name: "Gottardi",
twitter_username: null,
portfolio_url: "https://www.instagram.com/cristinagottardi/",
bio: "Hi! I’m an enthusiastic 28 year old Web Designer living and working in Milan. I'm from Trentino - in northern Italy - and I'm in love with the silence of landscapes and views hidden by my beloved mountains. ",
location: "Trento, North Italy",
links: 
self: "https://api.unsplash.com/users/cristina_gottardi",
html: "https://unsplash.com/@cristina_gottardi",
photos: "https://api.unsplash.com/users/cristina_gottardi/photos",
likes: "https://api.unsplash.com/users/cristina_gottardi/likes",
portfolio: "https://api.unsplash.com/users/cristina_gottardi/portfolio",
following: "https://api.unsplash.com/users/cristina_gottardi/following",
followers: "https://api.unsplash.com/users/cristina_gottardi/followers"
,
profile_image: 
small: "https://images.unsplash.com/profile-1509140709644-5b9d6cd408f9?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=32&w=32",
medium: "https://images.unsplash.com/profile-1509140709644-5b9d6cd408f9?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=64&w=64",
large: "https://images.unsplash.com/profile-1509140709644-5b9d6cd408f9?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=128&w=128"
,
instagram_username: "cristina.gottardi",
total_collections: 0,
total_likes: 291,
total_photos: 380,
accepted_tos: true,
for_hire: false
,
exif: 
make: "FUJIFILM",
model: "X-T10",
exposure_time: "1/420",
aperture: "9.0",
focal_length: "27.0",
iso: 400
,
location: 
title: "Lucca, LU, Italia",
name: "Lucca, LU, Italia",
city: "Lucca",
country: "Italia",
position: 
latitude: 43.84292,
longitude: 10.502698

,
views: 560463,
downloads: 1769

...

JSON 对象 urls 块包含许多 URL,但我想将其中 1 个 URL 的值存储在 currentURL 变量中。

请有人检查代码并通过建议更改我的代码来帮助我,以便我可以做到。

【问题讨论】:

【参考方案1】:

您需要访问 urls 对象以获取所需的图像大小。 对于这个例子,我使用了完整的。

currentUrl = response.getJSONObject("urls").getString("full")

【讨论】:

以上是关于当JSON对象URL字段在android studio中包含多个URL时,如何存储来自JSON对象请求的URL?的主要内容,如果未能解决你的问题,请参考以下文章

Android探究之Gson@SerializedName

在 Android 的 RESTFUL Web 服务的 URL 中传递 JSON 对象?

如何在 Android 中解析 JSON 数组(不是 Json 对象)

如何从android中的url获取json对象

同一个活动Android Studio中的两个JSON解析方法

Scala 一张图带你看懂什么是伴生对象