Java 这个应用程序需要很长时间才能完全运行
我的应用程序打开时没有崩溃,但它从不更新我的列表视图。这就是为什么它似乎在downloadtask类中挂断了。我让它记录网站的html,但它从来没有通过所有网站。好像它一直在收集垃圾,我不知道它为什么这么做。任何帮助都将不胜感激Java 这个应用程序需要很长时间才能完全运行,java,android,Java,Android,我的应用程序打开时没有崩溃,但它从不更新我的列表视图。这就是为什么它似乎在downloadtask类中挂断了。我让它记录网站的html,但它从来没有通过所有网站。好像它一直在收集垃圾,我不知道它为什么这么做。任何帮助都将不胜感激 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, titles);
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)");
listView.setAdapter(arrayAdapter);
updateListView();
DownloadTask task = new DownloadTask();
task.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty");
}
public void updateListView() {
Cursor c = articlesDB.rawQuery("SELECT * FROM articles", null);
int contentIndex = c.getColumnIndex("content");
int titleIndex = c.getColumnIndex("title");
if (c.moveToFirst()){
titles.clear();
content.clear();
do {
titles.add(c.getString(titleIndex));
content.add(c.getString(contentIndex));
}while(c.moveToNext());
arrayAdapter.notifyDataSetChanged();
}
}
public class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while(data != -1){
char current = (char) data;
result += current;
data = reader.read();
}
// Log.i("Result: ", result);
JSONArray jsonArray = new JSONArray(result);
int numberOfItems = 20;
if (jsonArray.length() < 20){
numberOfItems = jsonArray.length();
}
articlesDB.execSQL("DELETE FROM articles");
for (int i=0; i< numberOfItems; i++) {
String articleId = jsonArray.getString(i);
url = new URL("https://hacker-news.firebaseio.com/v0/item/" + articleId + ".json?print=pretty");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader reader1 = new InputStreamReader(inputStream);
data = reader1.read();
String articleInfo = "";
while (data != -1){
char current = (char) data;
articleInfo += current;
data = reader1.read();
}
//Log.i("Article info: ", articleInfo);
JSONObject jsonObject = new JSONObject(articleInfo);
String articleTitle = jsonObject.getString("title");
String articleUrl = jsonObject.getString("url");
//Log.i("info: ", articleTitle + " URL: " + articleUrl);
url = new URL(articleUrl);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream2 = urlConnection.getInputStream();
InputStreamReader reader2 = new InputStreamReader(inputStream2);
data = reader2.read();
String articleContent = "";
while (data != -1){
char current = (char) data;
articleContent += current;
data = reader2.read();
}
Log.i("articleContent: ", articleContent);
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, articleContent);
}
} 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();
}
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView ListView=(ListView)findViewById(R.id.ListView);
arrayAdapter=新的arrayAdapter(这是android.R.layout.simple\u list\u item\u 1,标题);
articlesDB=this.openOrCreateDatabase(“Articles”,MODE_PRIVATE,null);
execSQL(“如果不存在项目,则创建表(id INTEGER主键、articleId INTEGER、title VARCHAR、content VARCHAR)”;
setAdapter(arrayAdapter);
updateListView();
DownloadTask任务=新建DownloadTask();
任务。执行(“https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty");
}
public void updateListView(){
游标c=articlesDB.rawQuery(“从文章中选择*项”,null);
int contentIndex=c.getColumnIndex(“内容”);
int titleIndex=c.getColumnIndex(“标题”);
if(c.moveToFirst()){
标题。清除();
content.clear();
做{
titles.add(c.getString(titleIndex));
add(c.getString(contentIndex));
}而(c.moveToNext());
arrayAdapter.notifyDataSetChanged();
}
}
公共类DownloadTask扩展了AsyncTask{
@凌驾
受保护的字符串背景(字符串…字符串){
字符串结果=”;
网址;
HttpURLConnection-urlConnection=null;
试一试{
url=新url(字符串[0]);
urlConnection=(HttpURLConnection)url.openConnection();
InputStream in=urlConnection.getInputStream();
InputStreamReader reader=新的InputStreamReader(in);
int data=reader.read();
while(数据!=-1){
当前字符=(字符)数据;
结果+=电流;
data=reader.read();
}
//Log.i(“结果:”,结果);
JSONArray JSONArray=新JSONArray(结果);
int numberOfItems=20;
if(jsonArray.length()<20){
numberOfItems=jsonArray.length();
}
articlesDB.execSQL(“从条款中删除”);
对于(int i=0;i
请发布完整的代码您需要修改您的设计,启动异步任务以进行多个调用并处理来自同一数据库的数据库:O