Проблемы при использовании AsyncTask в Android Studion для двух URL - PullRequest
0 голосов
/ 25 марта 2020

У меня есть два URL для получения JSON данных.

String HttpJSonURL = "https:/.......quiz.php"
String HttpJsonCatQuizURL="https://.....catquiz.php"

Теперь я вызываю следующие методы

SaveButtonInSQLite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDataBaseBuild();
                SQLiteTableQuizBuild();
                SQLiteTableCatQuizBuild();
                DeletePreviousData();
                new StoreJSonDataInQuiz(MainActivity.this).execute();
                new StoreJSonDataInCatQuiz(MainActivity.this).execute();
            }
        });

И методы определяются как

private class StoreJSonDataInQuiz extends AsyncTask<Void, Void, Void> {

        public Context context;
        String FinalJSonResult;
        public StoreJSonDataInQuiz(Context context) {
            this.context = context;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setTitle("LOADING Quiz Data");
            progressDialog.setMessage("Please Wait");
            progressDialog.show();
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            HttpServiceClass httpServiceClass = new HttpServiceClass(HttpJSonURL);
            try {
                httpServiceClass.ExecutePostRequest();
                if (httpServiceClass.getResponseCode() == 200) {
                    FinalJSonResult = httpServiceClass.getResponse();
                    if (FinalJSonResult != null) {
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = new JSONArray(FinalJSonResult);
                            JSONObject jsonObject;
                            for (int i = 0; i < jsonArray.length(); i++) {
                                jsonObject = jsonArray.getJSONObject(i);
                                Integer catid = jsonObject.getInt("catid");
                                Integer id=jsonObject.getInt("_id");
                                String question=jsonObject.getString("question");
                                String answer = jsonObject.getString("answer");
                                String opta=jsonObject.getString("opta");
                                String optb=jsonObject.getString("optb");
                                String optc=jsonObject.getString("optc");
                                String optd=jsonObject.getString("optd");
                                String SQLiteDataBaseQueryHolder = "INSERT INTO "+SQLiteHelper.TABLE_QUIZ+"" +
                                        " (catid,_id,question,answer,opta,optb,optc,optd) VALUES( "
                                        +catid +", "+ id +" ,'" +question+"' "+
                                        " ,'" +answer+"' "+
                                        " ,'" +opta +"' "+
                                        " ,'" +optb+"' "+
                                        " ,'" +optc+"' "+
                                        " ,'" +optd +"' "+
                                        ");";
                                sqLiteDatabase.execSQL(SQLiteDataBaseQueryHolder);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                else {

                    Toast.makeText(context, httpServiceClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result)
        {
            sqLiteDatabase.close();
            progressDialog.dismiss();
            Toast.makeText(MainActivity.this,"Load Done", Toast.LENGTH_LONG).show();
        }
    }


    private class StoreJSonDataInCatQuiz extends AsyncTask<Void, Void, Void> {

        public Context context;
        String FinalJSonResult;
        public StoreJSonDataInCatQuiz(Context context) {
            this.context = context;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setTitle("LOADING CATQUIZ DATA");
            progressDialog.setMessage("Please Wait");
            progressDialog.show();
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            HttpServiceClass httpServiceClass = new HttpServiceClass(HttpJsonCatQuizURL);
            try {
                httpServiceClass.ExecutePostRequest();
                if (httpServiceClass.getResponseCode() == 200) {
                    FinalJSonResult = httpServiceClass.getResponse();
                    if (FinalJSonResult != null) {
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = new JSONArray(FinalJSonResult);
                            JSONObject jsonObject;
                            for (int i = 0; i < jsonArray.length(); i++) {
                                jsonObject = jsonArray.getJSONObject(i);
                                Integer id=jsonObject.getInt("_id");
                                String quizno=jsonObject.getString("quizno");
                                String SQLiteDataBaseQueryHolder = "INSERT INTO "+SQLiteHelper.TABLE_CATQUIZ+"" +
                                        " VALUES( "
                                        + id +" ,'" +quizno+"'); ";
                                sqLiteDatabase.execSQL(SQLiteDataBaseQueryHolder);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                else {

                    Toast.makeText(context, httpServiceClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result)
        {
         //   sqLiteDatabase.close();
            progressDialog.dismiss();
            Toast.makeText(MainActivity.this,"Load Done", Toast.LENGTH_LONG).show();
        }
    }

Когда я пишу новый StoreJSonDataInQuiz (MainActivity.this) .execute ();

ИЛИ

новый StoreJSonDataInCatQuiz (MainActivity.this) .execute ();

по отдельности, он правильно выбирает данные. Но когда обе строки выполняются последовательно, это не делает работу, и JSON данные не извлекаются из 2-го URL.

Пожалуйста, помогите

...