Android calling AsyncTask right after an another finished

后端 未结 7 2187
萌比男神i
萌比男神i 2020-11-28 06:15

I have some problem with Android AsyncTask. There is an Activity which contains some TextView a button and a picture. When an user entered this activity I start an asynctask

相关标签:
7条回答
  • 2020-11-28 06:33
    int count;
    
     private void attemptConnect() 
     {
       count = 0;
       str_lang = "English";
       str_wait = "Plaese Wait";
    
       new AllQuestion().execute();
    
    }
    
    
    private class AllQuestion extends AsyncTask<String, String, String> {
        ProgressDialog pg;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
            pg = new ProgressDialog(LanguageActivity.this);
            pg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pg.setMessage(str_wait);
            pg.setCancelable(false);
            pg.show();
        }
    
        @Override
        protected String doInBackground(String... strings) {
            try {
                SoapObject soapObject = new SoapObject(AppConstant.NAMESPACE, AppConstant.QUESTION_SOAP_METHOD); 
                soapObject.addProperty("language", str_lang);
    
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(soapObject);
    
                HttpTransportSE se = new HttpTransportSE(AppConstant.webUrl);
                se.call(AppConstant.QUESTION_SOAP_ACTION, envelope);
    
    
                Object responce = envelope.getResponse();
                Log.d("Question List:=>>", "" + responce);
    
                return responce.toString();
            } catch (Exception e) {
                e.printStackTrace();
                pg.dismiss();
                return null;
            }
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
    
            if (pg.isShowing()) {
                pg.dismiss();
                Log.i(TAG, s);
    
                if (s != null || !s.equalsIgnoreCase("")) {
                    try {
                        JSONArray array = new JSONArray(s);
    
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject obj = array.getJSONObject(i);
    
                            String queId = obj.getString(TAG_QID);
                            String que = obj.getString(TAG_QUE);
                            String str_Opt = obj.getString(TAG_OPT);
    
                            question = new Question(queId, que, str_lang, str_catId, str_Opt, manager.getDateTime());
                            helper.insertQuestion(question);
                        }
                        count++;
                        if (count < 5) {
                            if (count == 1) {
                                str_lang = "German";
                                str_wait = "bitte warte einen Moment";
    
                                    new AllQuestion().execute();
                            }
                            if (count == 2) {
                                str_lang = "Italian";
                                str_wait = "per favore aspetta un momento";
    
                                    new AllQuestion().execute();
                            }
                            if (count == 3) {
                                str_lang = "Chinese";
                                str_wait = "请稍候";
    
                                    new AllQuestion().execute();
                            }
                            if (count == 4) {
                                str_lang = "French";
                                str_wait = "patientez s'il-vous-plait";
    
                                    new AllQuestion().execute();
    
                        }
                        Log.d("All Question:-", question.toString());
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题