我正在使用newsapi开发新闻应用程序。我有一个主页可以下载前15条文章,而另一个页面可以从hackernews api下载相同的内容。每次我从Android Studio运行该应用程序时,都会在填充列表视图之前显示白屏几分钟。
我尝试限制下载速度,检查了SQLite索引和StackOverflow中的其他内容,但似乎无法解决我的问题。
主页
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
...
//START THE BACKGROUND TASK
task = new BackgroundTask();
String s = task.execute("https://newsapi.org/v2/top-headlines?country=us&apiKey").get();
...
//POPULATE LIST WITH DATABASE CONTENT
updateContent();
public void updateContent(){
...
if(cursor.moveToFirst()){
homeStories.clear();
homeLinks.clear();
}
if(cursor.getCount() > 0) {
do {
homeStories.add(cursor.getString(nameIndex));
homeLinks.add(cursor.getString(addressIndex));
} while (cursor.moveToNext());
adapter.notifyDataSetChanged();
}
//BACKGROUND TASK
public class BackgroundTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground (String...urls){
//GET JSON AND SUMMARIES IN SINGLE FOR LOOP FOR EACH ARTICLE
...
for (int i = 0; i < 16; i++) {
JSONObject content = jsonArray.getJSONObject(i);
...
//ADD TO DATABASE
database.execSQL("INSERT INTO trending (name, address) VALUES ('" + title + "','" + address + "')");
//GET THE SUMMARY OF EACH ARTICLE
url = new URL("https://www.summarizebot.com/api/summarize?...
JSONArray j2Array = new JSONArray(s1);
for (int j = 0; j < j2Array.length(); j++) {
JSONObject object2 = j2Array.getJSONObject(j);
s2 += object2.getString("sentence");
}
//END OF MAIN FOR LOOP
}
Hackernews页面
//SAME AS ABOVE
尝试进行故障排除后,我想到了几个问题: