开发者社区> 问答> 正文

提取数据时我的应用程序屏幕变黑

我的应用程序从gaurdian新闻api检索数据,并以列表形式显示数据,并使用了客户阵列适配器。每当我运行时,该应用程序什么都不会显示,只是空白。在构建或运行期间没有错误。我找不到我要去哪里了。

请帮忙...

MainActivity类

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);
    }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-27 22:33:23 387 0
1 条回答
写回答
取消 提交回答
  • 当项目添加到NewsAdapter时,您需要通知NewsAdapter。否则它将不知道要重画。假设NewsAdapter是RecyclerViewAdapter,请newsAdapter.notifyDataSetChanged()在的末尾调用onResponse。

    回答来源:Stack Overflow

    2020-03-27 22:33:35
    赞同 展开评论 打赏
问答分类:
API
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载