Android AsyncTask问题(连接到Web服务)

时间:2018-09-03 20:40:18

标签: java android web-services android-asynctask

因此我正在为应用程序进行登录活动,我想使用AsynTask连接到(SOAP)Web服务,我将向该Web服务发送密钥,用户名和密码,这将给我一个回报布尔值结果,表明用户是否有效。...在尝试AsyncTask之前,我使用的是在线找到的方法,在该方法中,您将创建一个-caller类-来扩展-Thread类-并在-MainActivity-中使用while循环, -UI线程进入睡眠状态-,它正在正常工作,但是感觉有点不正确,所以我调查了一下并发现了有关AsyncTask的信息,但我似乎无法使其工作... PS:自从以来,我无法共享Web服务的URL它属于我的大学,对此感到抱歉。

public class LoginActivity extends AppCompatActivity {

Button loginButton;
EditText usernumber,password;
boolean result;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    getSupportActionBar().hide();

    loginButton = (Button) findViewById(R.id.loginButton);
    usernumber = (EditText) findViewById(R.id.userNum);
    password = (EditText) findViewById(R.id.userPass);

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                new CallLoginWS().execute();

                if(result == true){
                    Intent bEmpIntent = new Intent(LoginActivity.this,BasicEmployeeActivity.class);
                    startActivity(bEmpIntent);
                }else{
                    Toast.makeText(LoginActivity.this,"username or password incorrect",Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });

}

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

    private final String SOAP_ACTION = "";
    private final String OPERATION_NAME = "IsValidUser";
    private final String WSDL_TARGET_NAMESPACE = "";
    private final String HOST = "";
    private final String FILE = "";
    private final String KEY = "";

    @Override
    protected Void doInBackground(Void... voids) {
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
        PropertyInfo pi=new PropertyInfo();
        pi.setName("usagekey");
        pi.setValue(KEY);
        pi.setType(String.class);
        request.addProperty(pi);
        pi = new PropertyInfo();
        pi.setName("username");
        pi.setValue(usernumber.getText().toString());
        pi.setType(String.class);
        request.addProperty(pi);
        pi = new PropertyInfo();
        pi.setName("password");
        pi.setValue(password.getText().toString());
        pi.setType(String.class);
        request.addProperty(pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpsTransportSE httpsTransport = new HttpsTransportSE(HOST,443,FILE,15000);

        SSLConnection ssl = new SSLConnection();
        ssl.allowAllSSL();
        try
        {
            httpsTransport.call(SOAP_ACTION, envelope);
            result = (boolean)envelope.getResponse();
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
        return null;
    }
}

}

对此是新手,现在我真的迷路了,因此非常感谢您的帮助/咨询。

1 个答案:

答案 0 :(得分:0)

您需要移动此代码

            if(result == true){
                Intent bEmpIntent = new Intent(LoginActivity.this,BasicEmployeeActivity.class);
                startActivity(bEmpIntent);
            }else{
                Toast.makeText(LoginActivity.this,"username or password incorrect",Toast.LENGTH_SHORT).show();
            }

到asynctask的onPostExecute()