如何在aysnctask内部执行长循环?

时间:2018-09-12 09:17:23

标签: android android-studio android-asynctask

我想在aysnc_task中执行以下代码。我尝试过,但后台没有运行完整的代码。请帮助我。有人指导我。我想在aysnc_task中执行以下代码。但是代码在postexecute中的webapi调用上崩溃。请帮助我。

 public class Asynctaskc extends AsyncTask<Void, Void, Void> {

        private ProgressDialog progressDialog;
        String rcode;
        String rResponseMesssage;
        CustomerValidationResponse customerValidationResponse;
        CustomerValidationRequest customerValidationRequest;
        TokenResponse tokenResponse;
        String tokens;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(Login_activity.this);
            progressDialog.setMessage("Saving image to SD Card");

            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
 @Override
        protected Void doInBackground(Void... voids) {

            final GenerateOAuthToken generateOAuthToken = new GenerateOAuthToken();

            generateOAuthToken.OAuthToken(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                    String tokenReponseJson = response.body().string();

                    //Json string to Class
                    tokenResponse = new Gson().fromJson(tokenReponseJson, TokenResponse.class);
                    tokens = tokenResponse.access_token;



                }
            });


            return null;
        }



    @Override
        protected void onPostExecute(Void vvoid) {
            super.onPostExecute(vvoid);

            customerValidationRequest = new CustomerValidationRequest(
                    usrname,
                    usrpass,
                    ConstantVariables.getAppversioncode(),
                    false
            );


            String JSONBody = new Gson().toJson(customerValidationRequest);

            final WebAPICall webAPICall = new WebAPICall();

            webAPICall.POSTCall(tokens, "customer/CustomerValidation", JSONBody, new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                    String responseJSON = response.body().string();
                    customerValidationResponse = new Gson().fromJson(responseJSON, CustomerValidationResponse.class);
                    rResponseMesssage = String.valueOf(customerValidationResponse.ResponseMessage);
                    rcode = String.valueOf(customerValidationResponse.ResponseCode);
                    //Json string to Class
                }
            });
            progressDialog.dismiss();
            if (rcode.equals(ConstantVariables.ResponseCodeSucess)) {

                ConstantVariables.seteEmail(usrname);

                startActivity(new Intent(getApplicationContext(), Navigationdrawer.class));
            } else {
                Toast.makeText(Login_activity.this, "" + rResponseMesssage, Toast.LENGTH_SHORT).show();
            }


        }
    }

0 个答案:

没有答案