0
votes

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RequestQueue requestQueue;
    requestQueue = Volley.newRequestQueue(this);
    List<news_Objects> newsList = new ArrayList<>();

    //url for News API 

    String url="https://newsapi.org/v2/everything?q=apple&from=2021-08-31&to=2021-08-31&sortBy=popularity&apiKey=ca3d6c89eff24db2a8ef78868f0af555";

//making json object request JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() { @Override public void onResponse(JSONObject response) {

            try {
                JSONArray jsonArr = response.getJSONArray("articles");

                for (int i = 0; i < jsonArr.length(); i++) {
                    JSONObject newsDetail = jsonArr.getJSONObject(i);

//class name news_Objects created

                    news_Objects news = new news_Objects();

                    String imtUrl = newsDetail.getString("urlToImage");
                    String title = newsDetail.getString("title");
                    String detail = newsDetail.getString("description");
                    String newsUrl = newsDetail.getString("url");
                    String content = newsDetail.getString("content");

                    news.setNewsImageUrl(imtUrl);
                    news.setNewsTitle(title);
                    news.setNewsDetail(detail);
                    news.setNewsUrl(newsUrl);
                    news.setContent(content);
                    newsList.add(news);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("fine", "Something Wrong"+error);
            Toast.makeText(MainActivity.this, "Something Wrong "+error, Toast.LENGTH_SHORT).show();
            error.printStackTrace();
        }
    });
    requestQueue.add(request);

}

}

2
Please format the code properly, add the stack trace of the error. It would help others understand your question better.Akhil
Can you add the error message and possibly stacktrace to your question?Ace Falobi
The URL also seems to be returning a 401 error saying the API Key is invalid. I think that might be your issueAce Falobi
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.Community

2 Answers

0
votes

Your API Key is Invalid please check your API key. Getting this response: {"status":"error","code":"apiKeyInvalid","message":"Your API key is invalid or incorrect. Check your key, or go to https://newsapi.org to create a free API key."}

0
votes

Maybe Your API key is not live, please check your API key status than again hit the api.