奇怪的Android AsyncTask bug我无法理解

时间:2011-10-21 14:03:45

标签: java android android-asynctask ksoap2

所以我使用AsyncTask来执行对我的Windows WCF服务的调用。到目前为止它已经很好了,我可以让它调用一个返回字符串并在应用程序中显示它的方法。但是当尝试返回bool的方法时,我会遇到一些错误..

代码

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

}

然而,每次运行RegisterSeat()方法时,代码都会停止并最终到达Catch()like thise is null内的最后一行。没有演示文稿? I can see the value of result.tostring()它是真或假,因为wcf返回它。但在某些地方,一切都有问题,我不知道该怎么做:/ !!

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

2 个答案:

答案 0 :(得分:1)

我要注意的一件事是你应该使用.equals(“true”)而不是“== true”来比较你的字符串

很高兴它有效:)

答案 1 :(得分:0)

我觉得你的设置中有些东西搞砸了。由于return falsebugged,因此程序无法像截图中那样真正到达断点true。此外,在您的第二个屏幕截图中,您还不在catch,而是在试用内部的return false。这个语句不会抛出异常,所以如果你最终陷入困境,那就太奇怪了。我的猜测是,部署的代码和IDE中的代码并不相同。如果我查看您粘贴的代码和截图的代码,它们也不相同。修复此部署问题,修复评论中提到的SnowyTracks点,看看会发生什么。