简体   繁体   中英

Android AsyncTask problem (connecting to a webservice)

so im working on the login activity for an app, and i wanna connect to a (SOAP) web service using AsynTask, i will be sending a key,username and a password to the web service that will give me in return a Boolean result whether the user is valid or not.... Before trying AsyncTask i was using a method i found online, where you create a -caller class- that extends -Thread class- and use a while loop inside -MainActivity- that makes the -UI Thread sleeps-, well it was working but it felt kinda not the right approach so i looked into it and found about AsyncTask but i cant seem to make it work... PS : i cant share the url for the webservice since it belongs to my university, sorry for that.

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;
    }
}

}

im new to this and im really lost right now, so any help/advice is highly appreciated.

You need to move this code

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

to onPostExecute() of your asynctask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM