Search code examples
javaandroidandroid-asynctaskksoap2

Weird Android AsyncTask bug I can't understand


So I'm using an AsyncTask to perform a call to my windows WCF service. So far it has gone great, I can get it to call a method that returns a string and display it within the application. But when trying to methods that return a bool i get some errors..

Code

RegisterSeatTask task = new RegisterSeatTask();
task.execute(this);

protected class RegisterSeatTask extends AsyncTask<Context, Integer, String> {
    protected String doInBackground(Context... arg0) {
try {
......
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

if (!LicenseHandler.RegisterSeat(licenseKey, ident,android.os.Build.DEVICE,tm.getDeviceId())) {
                    return "License is invalid or does not have any empty seats!";
}
......
}
protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
    }
}

public static boolean RegisterSeat(String License, String Identifier,
        String Device, String DeviceID) {
    try {

        METHOD_NAME = "RegisterSeat";
        SOAP_ACTION = "http://tempuri.org/IService/RegisterSeat";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("License", License);
        request.addProperty("MyIdentifier", Identifier);
        request.addProperty("Device", Device);
        request.addProperty("DeviceID", DeviceID);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
        if (result.toString().toLowerCase() == "true") {
            return true;
        }
        // to get the data
        return false;
    } catch (Exception e) {

        return false;
    }

}

However everytime i run the method RegisterSeat() the code stops and ends up on the last line inside the Catch() like this and e is null.. There are no expcetions cast? I can see the value of result.tostring() it is either true or false as the wcf returns it. But somewhere everything bugs out and I don't know what to make of it :/!!

like this e is null I can see the value of result.tostring()


Solution

  • one thing I would note is that you should be using .equals("true") rather than "== true" to compare your strings

    glad it worked :)