خرید بک لینک

Vote count: 0

Can anyone help me in fixing my code?

Apparently the problem is that I can't run more than one ASyncTask on my main thread. Can anyone give me some advise as to how I can fix my code? Thank you!

I apologize for not Idea my code. I can explain if you guys get confused while reading it.

 public class MainActivity extends AppCompatActivity {

    ListView listView;
    private SQLiteDatabase myDatabase;
    private Cursor cursor;
    boolean finished = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);
        myDatabase = this.openOrCreateDatabase("HackerNews", MODE_PRIVATE, null);

        Cursor cursor = myDatabase.rawQuery("SELECT * FROM ids", null);
        int index = cursor.getColumnIndex("urlID");
        cursor.moveToFirst();

        DownloadContent content = new DownloadContent();

        while(cursor != null){
                String newUrl = "https://hacker-news.firebaseio.com/v0/item/" + cursor.getString(index) + ".json?print=pretty";
                content.execute(newUrl);
                cursor.moveToNext();
        }
    }

    public class DownloadIDs extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            String result = "";
            URL url;
            HttpURLCoection urlCoection = null;
            try {
                url = new URL(params[0]);
                urlCoection = (HttpURLCoection) url.openCoection();
                InputStream inputStream = urlCoection.getInputStream();
                InputStreamReader reader = new InputStreamReader(inputStream);
                int data = reader.read();

                while (data >= 0) {
                    char current = (char) data;
                    result += current;
                    data = reader.read();
                }

                retu result;

            } catch (Exception e) {
                e.printStackTrace();
                retu "Fail";
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            myDatabase.execSQL("CREATE TABLE IF NOT EXISTS ids (id INTEGER PRIMARY KEY, urlID VARCHAR)");
            cursor = myDatabase.rawQuery("SELECT COUNT(*) FROM ids", null);
            cursor.moveToFirst();
            int count = cursor.getInt(0);

            if (!(count > 0)) {

                try {
                    JSONArray ids = new JSONArray(s);
                    for (int i = 0; i < ids.length(); i++) {
                        myDatabase.execSQL("INSERT INTO ids (urlID) VALUES ('" + ids.getString(i) + "')");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i("message", "TABLE1 IS NOT EMPTY");
            }
        }
    }

    public class DownloadContent extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String result = "";
            URL url;
            HttpURLCoection urlCoection = null;
            try {
                url = new URL(params[0]);
                urlCoection = (HttpURLCoection) url.openCoection();
                InputStream inputStream = urlCoection.getInputStream();
                InputStreamReader reader = new InputStreamReader(inputStream);
                int data = reader.read();

                while (data >= 0) {
                    char current = (char) data;
                    result += current;
                    data = reader.read();
                }

                retu result;

            } catch (Exception e) {
                e.printStackTrace();
                retu null;
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            myDatabase.execSQL("CREATE TABLE IF NOT EXISTS content(id INTEGER PRIMARY KEY, title VARCHAR, url VARCHAR)");
            cursor = myDatabase.rawQuery("SELECT COUNT(*) FROM content", null);
            cursor.moveToFirst();
            int count = cursor.getInt(0);

            if (!(count > 0)) {

                try {
                    JSONObject jsonObject = new JSONObject(s);
                    String title = jsonObject.getString("title");
                    String url = jsonObject.getString("url");

                    myDatabase.execSQL("INSERT INTO content (title, url) VALUES('" + title +"','" + url + "')");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i("mess2", "table 2 is NOT EMPTY");
            }
        }
    }
}

asked 3 mins ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 24 خرداد 1395 ساعت: 8:21

صفحه بندی