kotlin Bad URL Performing Second Volley Request Android Studio

cbjzeqam  于 2023-06-24  发布在  Kotlin
关注(0)|答案(1)|浏览(108)

新的移动的开发和沿着一组视频教程。我在活动开始时执行JSON Object请求。我需要执行第二个请求,但这次是使用从JSON对象获得的链接的图像。第一个请求基本上起作用了,除了一个TextViews之外,它更新了所有的TextViews。包含图像链接的TextView只是显示“https://”而不是整个链接,ImageView根本不会更改,因为图像请求由于“Bad URL”而失败。
这是我目前使用的版本:
implementation 'com.android.volley:volley:1.2.1'
我的第一个请求看起来像这样:

val request = JsonObjectRequest(Request.Method.GET, requestURL, null, { response ->

            for (i in 0 until response.length()) {
                txtName.text = getString(R.string.name,response.getString("Title"))
                txtRating.text = getString(R.string.rating,response.getString("Rated"))
                txtSummary.text = getString(R.string.summary, response.getString("Plot"))
                txtGenre.text = getString(R.string.genre,response.getString("Genre"))
                txtLength.text = getString(R.string.length,response.getString("Runtime"))           
                txtUrl.text = getString(R.string.url,response.getString("Poster"))

                Log.d("Poster Link", posterLink)
            }
            Log.d("Response", "$response")
        }, {

            Log.d("Error Message", "Error: ${it.message}")
        })

        request.setShouldCache(false)
        RequestHandler.getInstance(applicationContext).addToRequestQueue(request)

我把它放在onCreate里。该请求工作,它改变了大部分的TextView显示字段我请求除了链接TextView。如果这有帮助,在logcat中,这是我从这个特定搜索的请求中获得的信息:

{"Title":"Star Wars: Episode IV - A New Hope","Year":"1977","Rated":"PG","Released":"25 May 1977","Runtime":"121 min","Genre":"Action, Adventure, Fantasy","Director":"George Lucas","Writer":"George Lucas","Actors":"Mark Hamill, Harrison Ford, Carrie Fisher","Plot":"Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a Wookiee and two droids to save the galaxy from the Empire's world-destroying battle station, while also attempting to rescue Princess Leia from the mysterious Darth ...","Language":"English","Country":"United States","Awards":"Won 6 Oscars. 65 wins & 31 nominations total","Poster":"https:\/\/m.media-amazon.com\/images\/M\/MV5BOTA5NjhiOTAtZWM0ZC00MWNhLThiMzEtZDFkOTk2OTU1ZDJkXkEyXkFqcGdeQXVyMTA4NDI1NTQx._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.6\/10"},{"Source":"Rotten Tomatoes","Value":"93%"},{"Source":"Metacritic","Value":"90\/100"}],"Metascore":"90","imdbRating":"8.6","imdbVotes":"1,393,554","imdbID":"tt0076759","Type":"movie","DVD":"06 Dec 2005","BoxOffice":"$460,998,507","Production":"N\/A","Website":"N\/A","Response":"True"}

然后我有一个函数,它请求看起来像这样的图像:

fun imgRequest(link : String){
var img = createBitmap(1000,1000)

        val imageRequest = ImageRequest(link, { response ->
            img = response

            Log.d("Image Request Function", "Image requested")
        }, 0,  0, ImageView.ScaleType.FIT_CENTER, Bitmap.Config.ARGB_8888,
            {
                Log.d("Error Message", "Error: ${it.message}")
            })

        imageRequest.setShouldCache(false)
        RequestHandler.getInstance(applicationContext).addToRequestQueue(imageRequest)

        imgView.setImageBitmap(img)
}

我尝试在JSON对象请求之后调用函数,也尝试在对象请求之后丢弃函数并发布图像请求。它基本上只是应该改变我的ImageView,“imgView”,我请求的图像。我使用“Log.d”来查看它是否正确地获取了链接,在第一次请求期间的logcat中,仅包含链接的Log.d消息看起来像这样:https://m.media-amazon.com/images/M/MV5BOTA5NjhiOTAtZWM0ZC00MWNhLThiMzEtZDFkOTk2OTU1ZDJkXkEyXkFqcGdeQXVyMTA4NDI1NTQx._V1_SX300.jpg,当我点击它时,图像成功地弹出在一个新窗口中。虽然出于某种原因,在请求之外,当我将TextView.text设置为它时,它变成了“https://”。此外,Logcat在图像请求失败时向我提供以下消息:

[1094] NetworkDispatcher.processRequest: Unhandled exception java.lang.RuntimeException: Bad URL 
                                                                                                    java.lang.RuntimeException: Bad URL

我跟随的视频把这个任务放在视频的最后,作为一种独立的挑战,所以我不可能在那里找到解决方案。我试着移动图像请求,我试着在onCreate上面的脚本顶部声明Bitmap来存储图像,我试着在那里声明一个空的var字符串“link”,然后在对象响应之后将其更改为链接。我还尝试在JSON对象请求中执行图像请求,但这真的不起作用。我不知道什么是错的,我应该在哪里寻找解决这个问题。

gzszwxb4

gzszwxb41#

我的假设是,有一些特殊的字符需要在试图直接使用的URL中进行编码。这是一个很好的做法,以santise网址之前,启动一个图像的请求。
例如,在启动ImageRequest之前使用Uri.Builder().parse(amazonImgUrl).toString()对url进行santize。

相关问题