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



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

\............
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);
    }



}

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

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

p.S:另请参阅下面的链接。

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

在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();
}

最新更新