My app screen goes blank while fetching data

Bhuvan Soni :

My App retrieves data from gaurdian news api and shows it in the form of list and used an customer Array Adapter. Whenever I run, the app shows nothing but goes blank. There are no errors during build or run. I can't find where am I going wrong.

Please help...

MainActivity class

package com.example.newsapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private final String url = "https://content.guardianapis.com/search?api-key=98c2fc67-0080-4f70-a3b7-fe216828b82d";
    private ListView mListView;
    private ArrayList<News> news;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        news = new ArrayList<>();
        mListView = findViewById(R.id.list);
        NewsAdapter adapter = new NewsAdapter(this, news);
        mListView.setAdapter(adapter);
        RequestQueue queue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest;
        jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject rootResponses = response.getJSONObject("response");
                    JSONArray results = rootResponses.getJSONArray("results");
                    for(int i = 0; i<results.length(); i++){
                        JSONObject newsRoot = results.getJSONObject(i);
                        String title = newsRoot.getString("webTitle");
                        String category = newsRoot.getString("sectionName");
                        String date = newsRoot.getString("webPublicationDate");
                        News newsObj = new News(title, category, date);
                        news.add(newsObj);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", "Error" + error );
            }
        });
        queue.add(jsonObjectRequest);
    }
}
Gabe Sechan :

You need to notify the NewsAdapter when items have been added to it. Otherwise it won't know to redraw. Assuming NewsAdapter is a RecyclerViewAdapter, call newsAdapter.notifyDataSetChanged() at the end of onResponse.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238550&siteId=1