Anuj-Kumar-Sharma / NewsApp

Android app for displaying News in a RecyclerView
41 stars 45 forks source link

I am not able to fetch the data from the api #7

Open SurajVC27 opened 3 years ago

SurajVC27 commented 3 years ago

I am not able to fetch the data from the api. I am getting compelete blank screen after adding volley request in News app. Please can you help me as I have to submit Newapp as assignment very soon.

Vivek995378 commented 3 years ago

Me too I am getting a blank white screen instead of news

sumit619sharma commented 3 years ago

hey bro

I am not able to fetch the data from the api. I am getting compelete blank screen after adding volley request in News app. Please can you help me as I have to submit Newapp as assignment very soon.

hey bro I have 3 steps to solve this query

  1. Add this Header function
  2. Make JsonObjectRequest of type (object : )
  3. if your MainClass is abstract remove the abstract (Keyword)
SurajVC27 commented 3 years ago

Hi, I am getting Volley Error Occured . Please can you help me with it. I have added the Header function already. I have mentioned the Code below.

val queue = Volley.newRequestQueue (activity as Context)

val url = "https://newsapi.org/v2/top-headlines?country=in&apiKey=97027177615d46768cc3d8701f41cbff" if(InternetConnectionManager().checkConnectivity(activity as Context)){

val jsonObjectRequest = object :

JsonObjectRequest(Request.Method.GET, url, null, Response.Listener { try { progressLayout.visibility = View.GONE

        val success = it.getBoolean("success")

        if (success){
            val data = it.getJSONArray("data")
            for (i in 0 until data.length()){
                val newsJsonObject = data.getJSONObject(i)
                val newsObject = news(
                        newsJsonObject.getString("title"),
                        newsJsonObject.getString("authur"),
                        newsJsonObject.getString("description"),
                        newsJsonObject.getString("url"),
                        newsJsonObject.getString("urlToImage")
                )
                newsInfoList.add(newsObject)

                recyclerAdapter =

NewsDashboardRecyclerAdapter(activity as Context, newsInfoList)

                recyclerNewsDashboard.adapter = recyclerAdapter

                recyclerNewsDashboard.layoutManager = layoutManager

            }
        } else {
            Toast.makeText(activity as Context, "Some Error Has

occured!!", Toast.LENGTH_SHORT).show() } } catch (e: JSONException){ Toast.makeText(activity as Context, "Some unexpected error occurred!!", Toast.LENGTH_LONG).show() }

}, Response.ErrorListener {

    Toast.makeText(activity as Context, "Volley Error

occurred!!!", Toast.LENGTH_LONG).show()

}){
    override fun getHeaders(): MutableMap<String, String> {
        val headers = HashMap<String, String>()
        headers["Content-type"] = "application/json"
        headers["token"] = "97027177615d46768cc3d8701f41cbff"
        return headers

        return super.getHeaders()
    }
}

queue.add(jsonObjectRequest)

On Wed, Jun 23, 2021 at 4:27 PM sumit619sharma @.***> wrote:

hey bro

I am not able to fetch the data from the api. I am getting compelete blank screen after adding volley request in News app. Please can you help me as I have to submit Newapp as assignment very soon.

hey bro I have 3 steps to solve this query

  1. Add this Header function https://stackoverflow.com/questions/64585493/unexpected-response-code-403-while-fetching-data-from-apis
  2. Make JsonObjectRequest of type (object : )
  3. if your MainClass is abstract remove the abstract (Keyword)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Anuj-Kumar-Sharma/NewsApp/issues/7#issuecomment-866738623, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKPKZBTU3MSDI3ARBDOGNITTUG425ANCNFSM4674JGWQ .

MasterBlaster99 commented 3 years ago

@SurajVC27 use retrofit instead of volley