webservice调用时如何添加进度条?

how to add progress bar while webservice call?

而 运行 此代码出现空指针错误!请给我解决方案。

\............
new LoginCheck().execute(txt_username.getText().toString(),txt_password.getText().toString());
.............//

class LoginCheck extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.VISIBLE);
        super.onPreExecute();

    }
    protected String         doInBackground(String... params) {

        String responsetring = "";
        try {
            SoapObject request = new SoapObject(NAMESPACE, "Logincheck");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            request.addProperty("username",params[0]);
            request.addProperty("password",params[1]);
            //Msg("Version"+appversion+"AppName"+Appname,Toast.LENGTH_LONG);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(NAMESPACE + "Logincheck", envelope);
            } catch (IOException | XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Msg(e.toString(), Toast.LENGTH_LONG);
            }
            SoapPrimitive response;
            try {
                response = (SoapPrimitive) envelope.getResponse();
                responsetring = response.toString();
            //    Msg(responsetring.toString(), 1);
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Msg(e.toString(), Toast.LENGTH_LONG);
            }
            String[] splitrows = responsetring.split(";");
            String usernamestr = "";
            String deviceid = "";
            String isanydevice = "";

            if (responsetring.equals("false")) {
           //     Helper.InfoMsg("Alert", "Please Check userame and password and confirm device is valid", LoginActivity.this);

            } else {
                usernamestr = splitrows[0];
                deviceid = splitrows[1];
                isanydevice = splitrows[2];
                TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                if (deviceid.equals(tm.getDeviceId().toString())) {
                    LoginActivity.this.username = usernamestr;
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_in, R.anim.left_out);
                } else {
                    TelephonyManager tm1 = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
              //      Helper.InfoMsg("Your Device Id", (tm1.getDeviceId().toString()), LoginActivity.this);
                    txt_warning.setText((tm1.getDeviceId().toString()));
                    if (isanydevice.equalsIgnoreCase("true")) {
                        LoginActivity.this.username = usernamestr;
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.right_in, R.anim.left_out);
                    } else {
                    //    Helper.warning("Sorry", "Not Allowed To Login  Other devices", LoginActivity.this);
                        // return false;
                    }
                }

            }
            //return true;
        } catch (Exception e) {
            Msg(e.toString(), 1);
            //  return false;
        }
        return null;
    }


    protected void onPostExecute(String result) {

        progressBar.setVisibility(View.INVISIBLE);
    }






}

在 onPreExecute 中:

 AlertDialog progressDialog = new ProgressDialog(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        progressDialog.setMessage("Please Wait...");
        progressDialog.show();

在onPostExecute

 progressDialog.dismiss();

在onPreExecute()

private ProgressDialog pdia;

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        pdia = new ProgressDialog(LoginActivity.this);
        pdia.setMessage("Loading, Please Wait...");
        pdia.show();
    }

在onPostExecute()中

   protected void onPostExecute(String result) {

    super.onPostExecute(result);
        pdia.dismiss();
}

所有代码:

class LoginCheck extends AsyncTask<String, String, String> {

private ProgressDialog pdia;

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        pdia = new ProgressDialog(LoginActivity.this);
        pdia.setMessage("Loading, Please Wait...");
        pdia.show();
    }

protected String doInBackground(String... params) {

    String responsetring = "";
    try {
        SoapObject request = new SoapObject(NAMESPACE, "Logincheck");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        request.addProperty("username",params[0]);
        request.addProperty("password",params[1]);
        //Msg("Version"+appversion+"AppName"+Appname,Toast.LENGTH_LONG);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
            androidHttpTransport.call(NAMESPACE + "Logincheck", envelope);
        } catch (IOException | XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Msg(e.toString(), Toast.LENGTH_LONG);
        }
        SoapPrimitive response;
        try {
            response = (SoapPrimitive) envelope.getResponse();
            responsetring = response.toString();
        //    Msg(responsetring.toString(), 1);
        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Msg(e.toString(), Toast.LENGTH_LONG);
        }
        String[] splitrows = responsetring.split(";");
        String usernamestr = "";
        String deviceid = "";
        String isanydevice = "";

        if (responsetring.equals("false")) {
       //     Helper.InfoMsg("Alert", "Please Check userame and password and confirm device is valid", LoginActivity.this);

        } else {
            usernamestr = splitrows[0];
            deviceid = splitrows[1];
            isanydevice = splitrows[2];
            TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            if (deviceid.equals(tm.getDeviceId().toString())) {
                LoginActivity.this.username = usernamestr;
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.right_in, R.anim.left_out);
            } else {
                TelephonyManager tm1 = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
          //      Helper.InfoMsg("Your Device Id", (tm1.getDeviceId().toString()), LoginActivity.this);
                txt_warning.setText((tm1.getDeviceId().toString()));
                if (isanydevice.equalsIgnoreCase("true")) {
                    LoginActivity.this.username = usernamestr;
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_in, R.anim.left_out);
                } else {
                //    Helper.warning("Sorry", "Not Allowed To Login  Other devices", LoginActivity.this);
                    // return false;
                }
            }

        }
        //return true;
    } catch (Exception e) {
        Msg(e.toString(), 1);
        //  return false;
    }
    return null;
}


protected void onPostExecute(String result) {

    super.onPostExecute(result);
        pdia.dismiss();
}

您在 Asynctask 中唯一错过的是,您忘记重写 onProgressUpdate() 方法。 onProgressUpdate() 方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。我在这里给出了示例代码。参考它。希望对您有所帮助。

 protected void onProgressUpdate(String... progress) 
{        
    progressBar.setProgress(Integer.parseInt(progress[0]));
} 

P.S: 另见下文link.

http://www.coderanch.com/t/612017/Android/Mobile/AsyncTask-show-progress-bar