应用程序正在永远运行完全

时间:2017-03-04 19:57:26

标签: java android

我的应用程序在没有崩溃的情况下打开,但它永远不会更新我的列表视图。这就是为什么它似乎挂在downloadtask类中。我有它记录网站的HTML,它永远不会通过所有这些。它似乎不断收集垃圾,我不知道为什么这样做。任何帮助将非常感激!

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

0 个答案:

没有答案