- 我 想 从 news Api 获取 数据 , 但 我 的 日志 猫 显示 错误 E/Volley :[ 44820 ] 网络 实用 程序 。 应该 重试 异常 :意外 响应 代码 403 , 请 告诉 我 此 问题 的 解决 方案 * *
This is a news app whose aim is to fetches data from newsApi.org and puts the image title on the recycler view of TextViews...... but while I m running this app on my android device this app is showing an error in the log and my application is doing noting
- 主要 活动 代码 * *
package com.example.newsfeeds
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var mAdapter: NewsListAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
fetchData()
mAdapter = NewsListAdapter()
recyclerView.adapter = mAdapter
}
private fun fetchData(){
val url = "https://newsapi.org/v2/top-headlines?sources=bbc-news&apiKey=cedef5f84e094e23a55e8de8120243ba"
val newsJsonObjectRequest = JsonObjectRequest(
Request.Method.GET,
url,
null,
Response.Listener {
val newsJsonArray = it.getJSONArray("articles")
val newsArray = ArrayList<News>()
for(i in 0 until newsJsonArray.length()){
val newsJsonObject = newsJsonArray.getJSONObject(i)
val news = News(
newsJsonObject.getString("author"),
newsJsonObject.getString("title"),
newsJsonObject.getString("url"),
newsJsonObject.getString("imageToUrl")
)
Toast.makeText(this,"Entered",Toast.LENGTH_LONG).show()
newsArray.add(news)
}
mAdapter.update(newsArray)
},Response.ErrorListener {
}
)
val queue = MySingleton.getInstance(this).requestQueue
queue.add(newsJsonObjectRequest)
}
}
中 的 每 一 个
- 适配 器 代码 * *
package com.example.newsfeeds
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.single_item_look.view.*
class NewsListAdapter() : RecyclerView.Adapter<NewsViewHolder>() {
private var items : ArrayList<News> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.single_item_look,parent,false)
return NewsViewHolder(view)
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
val currentItem = items[position]
holder.titleView.text = currentItem.title
}
fun update(updatedItems : ArrayList<News>){
items.clear()
items.addAll(updatedItems)
notifyDataSetChanged()
}
}
class NewsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val titleView : TextView = itemView.title
}
格式
- 我 的 单身 代码 * *
package com.example.newsfeeds
import android.content.Context
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.ImageLoader
import com.android.volley.toolbox.Volley
class MySingleton constructor(context: Context) {
companion object {
@Volatile
private var INSTANCE: MySingleton? = null
fun getInstance(context: Context) =
INSTANCE ?: synchronized(this) {
INSTANCE ?: MySingleton(context).also {
INSTANCE = it
}
}
}
val requestQueue: RequestQueue by lazy {
Volley.newRequestQueue(context.applicationContext)
}
fun <T> addToRequestQueue(req: Request<T>) {
requestQueue.add(req)
}
}
格式
- 新闻 代码 * *
package com.example.newsfeeds
data class News(val author : String ,
val title : String,
val url :String,
val urlToImage : String)
格式
1条答案
按热度按时间vql8enpb1#
覆盖getHeaders()方法并将Mozilla/5.0作为用户代理传递,不要忘记在URL中将API-KEY替换为您的API密钥