使用 Volley 进行查询时未从 JSON 文件获取数据
Not getting data from JSON file while questing using Volley
我正在使用 YouTube 数据 API 来获取 YouTube 视频的详细信息。
我正在使用这个 url = "https://www.googleapis.com/youtube/v3/videosid=JbhBdOfMEPs&key=API_KEY&part=snippet"
这是我从 API
请求的主要 kt 文件
import android.app.VoiceInteractor
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.options.IFramePlayerOptions.Companion.default
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
import org.json.JSONException
import org.json.JSONObject
import java.lang.Exception
class MathPlayer2 : AppCompatActivity() {
lateinit var query : RequestQueue
lateinit var details:ArrayList<data_math>
lateinit var youtubeNewPlayer:YouTubePlayerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_math_player2)
youtubeNewPlayer = findViewById(R.id.youtubeNewplayer)
initializingPlayer(youtubeNewPlayer,"JbhBdOfMEPs")
query = Volley.newRequestQueue(this)
val uid_array = arrayOf("JbhBdOfMEPs" , "rV7WjNWHOsI" , "fr04p6Ar9ic" , "qyW2mWvvtZ8",
"AkWtUOwlUgs","jC4SWFag6Qw","n9fgcm0Pwgs","-Xt4UDk7Kzw","YFyOsvnr9ig")
details = ArrayList<data_math>()
for(x in uid_array) {
gettingRequest(x)
}
if(details.isEmpty()){
println("///////////////////////////////////////////////")
println("EMPTY")
}
val recyclerView : RecyclerView = findViewById(R.id.mathRecyclerView)
val itemAdapter = Math_Recycler_Adapter(details)
val layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL , false)
recyclerView.layoutManager = layoutManager
recyclerView.itemAnimator = DefaultItemAnimator()
recyclerView.adapter = itemAdapter
}
//Herer I AM REQUESTING
private fun gettingRequest(x:String)
{
val url :String = "https://www.googleapis.com/youtube/v3/videos?id=" + x+
"&key=API_KEY&part=snippet"
val jsonObject = JsonObjectRequest(Request.Method.GET, url,null ,Response.Listener<JSONObject> {
val success = it.getBoolean("success")
if(success)
try{
val items = it.getJSONArray("items")
val item = items.getJSONObject(0)
val uid = item.getString("id")
val snippet = item.getJSONObject("snippet")
val title_video = snippet.getString("title")
val description = snippet.getString("description")
val thumbnails = snippet.getJSONObject("thumbnails")
val maxres = thumbnails.getJSONObject("maxres")
val url = maxres.getString("url")
val width = maxres.getInt("width")
val height = maxres.getInt("height")
details.add(data_math(uid,title_video))
}`enter code here`
catch (e:JSONException){
Toast.makeText(this,"error",Toast.LENGTH_LONG).show()
}
}, { error -> error.printStackTrace() })
query.add(jsonObject)
}
private fun initializingPlayer(youtubePlayer: YouTubePlayerView, str:String){
youtubePlayer.addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {
override fun onReady(@NonNull youTubePlayer: YouTubePlayer) {
val videoId = str
youTubePlayer.cueVideo(videoId, 0f)
}
})
youtubePlayer.getPlayerUiController().setFullScreenButtonClickListener(View.OnClickListener {
if (youtubePlayer.isFullScreen()) {
youtubePlayer.exitFullScreen()
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
// Show ActionBar
if (supportActionBar != null) {
supportActionBar!!.show()
}
} else {
youtubePlayer.enterFullScreen()
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
// Hide ActionBar
if (supportActionBar != null) {
supportActionBar!!.hide()
}
}
})
}
}
这是我的 XML 主文件 activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MathPlayer2">
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/youtubeNewplayer"
>
</com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mathRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/youtubeNewplayer"
>
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
这是我的数据class
package com.example.internproject1
import android.media.Image
import android.widget.ImageView
data class data_math(val uid:String, val title:String) {
}
//, val description:String ,val url :String , val width:Int,val height :Int
这是我的 recyclerView Adapter 文件
package com.example.internproject1
import android.app.PendingIntent.getActivity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.squareup.picasso.Picasso
import org.w3c.dom.Text
class Math_Recycler_Adapter(val details:ArrayList<data_math>):RecyclerView.Adapter<Math_Recycler_Adapter.MathViewHolder>() {
class MathViewHolder(v: View) :RecyclerView.ViewHolder(v) {
var VideoTitle:TextView = v.findViewById(R.id.math_title)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MathViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.math_recycler_view , parent,false)
return MathViewHolder(itemView)
}
override fun onBindViewHolder(holder: MathViewHolder, position: Int) {
val detail = details[position]
holder.VideoTitle.text = detail.title
}
override fun getItemCount(): Int {
return details.size
}
}
这是 RecyclerView XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
app:cardCornerRadius="15dp"
app:cardElevation="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/math_thumbnail"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_margin="10dp"
android:padding="10dp"
android:src="@drawable/digital_learning"
android:scaleType= "fitCenter"
android:foregroundGravity="center"
/>
<TextView
android:id="@+id/math_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textSize="22sp"
android:textAlignment="textEnd"
android:layout_below="@+id/math_thumbnail"
/>
<ImageView
android:id="@+id/btn_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/more"
android:layout_alignRight="@+id/math_title"
android:layout_alignTop="@+id/math_title"/>
<TextView
android:id="@+id/math_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:textSize="22sp"
android:layout_below="@id/math_title"
android:gravity="center"
android:textAlignment="center"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是api数据
{
"kind": "youtube#videoListResponse",
"etag": "AQ42JMutl9MwSwr5zWkZJNiFoaM",
"items": [
{
"kind": "youtube#video",
"etag": "6F4WX_k2_NkRd0js2j7nAdAkhEA",
"id": "JbhBdOfMEPs",
"snippet": {
"publishedAt": "2017-08-02T16:44:44Z",
"channelId": "UC0cd_-e49hZpWLH3UIwoWRA",
"title": "Introduction to Mathematics",
"description": "Everyone hates math, right? Wrong! I like math. Mathematicians like math, presumably. Physicists like math, usually. People who hate math just don't understand it. But like that weird ethnic food you're scared of trying or that odd kid in class that doesn't say much, if you come to understand something it's not so scary. In fact, if we can learn enough about math, we will come to love it",
"thumbnails": {
"default": {
"url": "<https://i.ytimg.com/vi/JbhBdOfMEPs/default.jpg>",
"width": 120,
"height": 90
},
"medium": {
"url": "<https://i.ytimg.com/vi/JbhBdOfMEPs/mqdefault.jpg>",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/JbhBdOfMEPs/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/JbhBdOfMEPs/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/JbhBdOfMEPs/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "Professor Dave Explains",
"tags": [
"dave farina",
"professor dave",
"professor dave explains",
"math",
"mathematics",
"algebra",
"geometry",
"trigonometry",
"calculus",
"linear algebra",
"differential equations",
"arithmetic",
"math class",
"math practice",
"newton's laws",
"equation",
"addition",
"subtraction",
"mathematician",
"subjective",
"objective",
"inarguable answers",
"observation",
"how much tip to leave"
],
"categoryId": "27",
"liveBroadcastContent": "none",
"localized": {
"title": "Introduction to Mathematics",
"description": "Everyone hates math, right? Wrong! I like math. Mathematicians like math, presumably. Physicists like math, usually. People who hate math just don't understand it. But like that weird ethnic food you're scared of trying, or that odd kid in class that doesn't say much, if you come to understand something it's not so scary. In fact, if we can learn enough about math, we will come to love it, because there is so much we can do with it. Watch this series to supplement your math classes, or just to learn enough math that you can become friends. It starts out super easy so don't be afraid! Okay"
},
"defaultAudioLanguage": "en"
}
}
],
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
}
}
在 Gradle 构建之后,我得到了 详细信息 ( 我在其中一个一个地存储所有获取的数据)空 我认为主 kt 文件中的 gettingRequest() 函数存在问题。谁能帮我找出问题所在?
api 数据中没有成功
所以删除这个
val success = it.getBoolean("success")
if(success)
我正在使用 YouTube 数据 API 来获取 YouTube 视频的详细信息。 我正在使用这个 url = "https://www.googleapis.com/youtube/v3/videosid=JbhBdOfMEPs&key=API_KEY&part=snippet"
这是我从 API
请求的主要 kt 文件
import android.app.VoiceInteractor
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.options.IFramePlayerOptions.Companion.default
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
import org.json.JSONException
import org.json.JSONObject
import java.lang.Exception
class MathPlayer2 : AppCompatActivity() {
lateinit var query : RequestQueue
lateinit var details:ArrayList<data_math>
lateinit var youtubeNewPlayer:YouTubePlayerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_math_player2)
youtubeNewPlayer = findViewById(R.id.youtubeNewplayer)
initializingPlayer(youtubeNewPlayer,"JbhBdOfMEPs")
query = Volley.newRequestQueue(this)
val uid_array = arrayOf("JbhBdOfMEPs" , "rV7WjNWHOsI" , "fr04p6Ar9ic" , "qyW2mWvvtZ8",
"AkWtUOwlUgs","jC4SWFag6Qw","n9fgcm0Pwgs","-Xt4UDk7Kzw","YFyOsvnr9ig")
details = ArrayList<data_math>()
for(x in uid_array) {
gettingRequest(x)
}
if(details.isEmpty()){
println("///////////////////////////////////////////////")
println("EMPTY")
}
val recyclerView : RecyclerView = findViewById(R.id.mathRecyclerView)
val itemAdapter = Math_Recycler_Adapter(details)
val layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL , false)
recyclerView.layoutManager = layoutManager
recyclerView.itemAnimator = DefaultItemAnimator()
recyclerView.adapter = itemAdapter
}
//Herer I AM REQUESTING
private fun gettingRequest(x:String)
{
val url :String = "https://www.googleapis.com/youtube/v3/videos?id=" + x+
"&key=API_KEY&part=snippet"
val jsonObject = JsonObjectRequest(Request.Method.GET, url,null ,Response.Listener<JSONObject> {
val success = it.getBoolean("success")
if(success)
try{
val items = it.getJSONArray("items")
val item = items.getJSONObject(0)
val uid = item.getString("id")
val snippet = item.getJSONObject("snippet")
val title_video = snippet.getString("title")
val description = snippet.getString("description")
val thumbnails = snippet.getJSONObject("thumbnails")
val maxres = thumbnails.getJSONObject("maxres")
val url = maxres.getString("url")
val width = maxres.getInt("width")
val height = maxres.getInt("height")
details.add(data_math(uid,title_video))
}`enter code here`
catch (e:JSONException){
Toast.makeText(this,"error",Toast.LENGTH_LONG).show()
}
}, { error -> error.printStackTrace() })
query.add(jsonObject)
}
private fun initializingPlayer(youtubePlayer: YouTubePlayerView, str:String){
youtubePlayer.addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {
override fun onReady(@NonNull youTubePlayer: YouTubePlayer) {
val videoId = str
youTubePlayer.cueVideo(videoId, 0f)
}
})
youtubePlayer.getPlayerUiController().setFullScreenButtonClickListener(View.OnClickListener {
if (youtubePlayer.isFullScreen()) {
youtubePlayer.exitFullScreen()
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
// Show ActionBar
if (supportActionBar != null) {
supportActionBar!!.show()
}
} else {
youtubePlayer.enterFullScreen()
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
// Hide ActionBar
if (supportActionBar != null) {
supportActionBar!!.hide()
}
}
})
}
}
这是我的 XML 主文件 activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MathPlayer2">
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/youtubeNewplayer"
>
</com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mathRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/youtubeNewplayer"
>
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
这是我的数据class
package com.example.internproject1
import android.media.Image
import android.widget.ImageView
data class data_math(val uid:String, val title:String) {
}
//, val description:String ,val url :String , val width:Int,val height :Int
这是我的 recyclerView Adapter 文件
package com.example.internproject1
import android.app.PendingIntent.getActivity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.squareup.picasso.Picasso
import org.w3c.dom.Text
class Math_Recycler_Adapter(val details:ArrayList<data_math>):RecyclerView.Adapter<Math_Recycler_Adapter.MathViewHolder>() {
class MathViewHolder(v: View) :RecyclerView.ViewHolder(v) {
var VideoTitle:TextView = v.findViewById(R.id.math_title)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MathViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.math_recycler_view , parent,false)
return MathViewHolder(itemView)
}
override fun onBindViewHolder(holder: MathViewHolder, position: Int) {
val detail = details[position]
holder.VideoTitle.text = detail.title
}
override fun getItemCount(): Int {
return details.size
}
}
这是 RecyclerView XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
app:cardCornerRadius="15dp"
app:cardElevation="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/math_thumbnail"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_margin="10dp"
android:padding="10dp"
android:src="@drawable/digital_learning"
android:scaleType= "fitCenter"
android:foregroundGravity="center"
/>
<TextView
android:id="@+id/math_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textSize="22sp"
android:textAlignment="textEnd"
android:layout_below="@+id/math_thumbnail"
/>
<ImageView
android:id="@+id/btn_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/more"
android:layout_alignRight="@+id/math_title"
android:layout_alignTop="@+id/math_title"/>
<TextView
android:id="@+id/math_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:textSize="22sp"
android:layout_below="@id/math_title"
android:gravity="center"
android:textAlignment="center"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是api数据
{
"kind": "youtube#videoListResponse",
"etag": "AQ42JMutl9MwSwr5zWkZJNiFoaM",
"items": [
{
"kind": "youtube#video",
"etag": "6F4WX_k2_NkRd0js2j7nAdAkhEA",
"id": "JbhBdOfMEPs",
"snippet": {
"publishedAt": "2017-08-02T16:44:44Z",
"channelId": "UC0cd_-e49hZpWLH3UIwoWRA",
"title": "Introduction to Mathematics",
"description": "Everyone hates math, right? Wrong! I like math. Mathematicians like math, presumably. Physicists like math, usually. People who hate math just don't understand it. But like that weird ethnic food you're scared of trying or that odd kid in class that doesn't say much, if you come to understand something it's not so scary. In fact, if we can learn enough about math, we will come to love it",
"thumbnails": {
"default": {
"url": "<https://i.ytimg.com/vi/JbhBdOfMEPs/default.jpg>",
"width": 120,
"height": 90
},
"medium": {
"url": "<https://i.ytimg.com/vi/JbhBdOfMEPs/mqdefault.jpg>",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/JbhBdOfMEPs/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/JbhBdOfMEPs/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/JbhBdOfMEPs/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "Professor Dave Explains",
"tags": [
"dave farina",
"professor dave",
"professor dave explains",
"math",
"mathematics",
"algebra",
"geometry",
"trigonometry",
"calculus",
"linear algebra",
"differential equations",
"arithmetic",
"math class",
"math practice",
"newton's laws",
"equation",
"addition",
"subtraction",
"mathematician",
"subjective",
"objective",
"inarguable answers",
"observation",
"how much tip to leave"
],
"categoryId": "27",
"liveBroadcastContent": "none",
"localized": {
"title": "Introduction to Mathematics",
"description": "Everyone hates math, right? Wrong! I like math. Mathematicians like math, presumably. Physicists like math, usually. People who hate math just don't understand it. But like that weird ethnic food you're scared of trying, or that odd kid in class that doesn't say much, if you come to understand something it's not so scary. In fact, if we can learn enough about math, we will come to love it, because there is so much we can do with it. Watch this series to supplement your math classes, or just to learn enough math that you can become friends. It starts out super easy so don't be afraid! Okay"
},
"defaultAudioLanguage": "en"
}
}
],
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
}
}
在 Gradle 构建之后,我得到了 详细信息 ( 我在其中一个一个地存储所有获取的数据)空 我认为主 kt 文件中的 gettingRequest() 函数存在问题。谁能帮我找出问题所在?
api 数据中没有成功 所以删除这个
val success = it.getBoolean("success")
if(success)