我正在尝试在android studio上构建新闻阅读器应用程序,但该应用程序显示空白屏幕或有时加载2或5个新闻标题。如何使其正常,快速地工作!
public class MainActivity extends AppCompatActivity {
ArrayList<String> titles=new ArrayList<>();
ArrayList<String> contents=new ArrayList<>();
ArrayAdapter<String> adapter;
SQLiteDatabase articlesDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=(ListView) findViewById(R.id.listView);
adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,titles);
listView.setAdapter(adapter);
articlesDB=this.openOrCreateDatabase("Articles",MODE_PRIVATE,null);
articlesDB.execSQL("CREATE TABLE IF NOT EXISTS articles(id INTEGER PRIMARY KEY,articleId INTEGER,title VARCHAR,content VARCHAR)");
updateListView();
DownloadTask task=new DownloadTask();
try {
task.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty");
}
catch (Exception e){
e.printStackTrace();
}
}
public void updateListView(){
Cursor c=articlesDB.rawQuery("SELECT * FROM articles",null);
int titleIndex=c.getColumnIndex("title");
int contentIndex=c.getColumnIndex("content");
if(c.moveToFirst()){
titles.clear();
contents.clear();
do{
titles.add(c.getString(titleIndex));
contents.add(c.getString(contentIndex));
}
while (c.moveToNext());
adapter.notifyDataSetChanged();
}
}
public class DownloadTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... urls) {
String result="";
URL url;
HttpURLConnection conn=null;
try {
url=new URL(urls[0]);
conn=(HttpURLConnection) url.openConnection();
InputStream inputStream=conn.getInputStream();
InputStreamReader reader=new InputStreamReader(inputStream);
int data=reader.read();
while (data!=-1){
char current=(char) data;
result=result+current;
data=reader.read();
}
JSONArray jsonArray=new JSONArray(result);
int numberOfItems=20;
if(jsonArray.length()<20){
numberOfItems=jsonArray.length();
}
articlesDB.execSQL("DELETE FROM articles");
for(int i=1;i<numberOfItems;i++){
String articleId=jsonArray.getString(i);
url=new URL("https://hacker-news.firebaseio.com/v0/item/"+articleId+".json?print=pretty");
conn=(HttpURLConnection) url.openConnection();
inputStream=conn.getInputStream();
reader=new InputStreamReader(inputStream);
data=reader.read();
String artcileInfo="";
while (data!=-1){
char current=(char) data;
artcileInfo=artcileInfo+current;
data=reader.read();
}
JSONObject jsonObject=new JSONObject(artcileInfo);
//Dont trust API might be possible there is no url or no title then it would throw an exception
if(!jsonObject.isNull("title") && !jsonObject.isNull("url")){
String articleTitle=jsonObject.getString("title");
String articleURL=jsonObject.getString("url");
url=new URL(articleURL);
conn=(HttpURLConnection) url.openConnection();
inputStream=conn.getInputStream();
reader=new InputStreamReader(inputStream);
data=reader.read();
String articleActualContent="";
while (data!=-1){
char current=(char) data;
articleActualContent=articleActualContent+current;
data=reader.read();
}
String sql="INSERT INTO articles(articleID,title,content) VALUES(? , ? , ?)";
SQLiteStatement statement=articlesDB.compileStatement(sql);
statement.bindString(1,articleId);
statement.bindString(2,articleTitle);
statement.bindString(3,articleURL);
statement.execute();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
updateListView();
}
}
}
GC中的输出释放了日志中持续出现的错误。 我的模拟器中需要19个新闻标题。