我无法理解奇怪的 Android AsyncTask bug

发布于 2024-12-11 08:33:57 字数 2463 浏览 0 评论 0原文

因此,我使用 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() 像这样e是null.. 没有任何期待吗? 我可以看到 result.tostring() 的值 它与 wcf 一样为 true 或 false返回它。但在某个地方一切都出了问题,我不知道该怎么办:/!!

像这样 e 为空 我可以看到 result.tostring() 的值

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就是爱搞怪 2024-12-18 08:33:57

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

很高兴它有效:)

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

glad it worked :)

悲念泪 2024-12-18 08:33:57

我认为你的设置有些混乱。程序不可能像屏幕截图中那样真正到达断点 return false,因为 buggedtrue。此外,在第二个屏幕截图中,您尚未进入 catch 状态,而是处于 try 内部的 return false 状态。这个语句不会抛出异常,所以如果你最终陷入了 catch 中,那就很奇怪了。我的猜测是部署的代码和 IDE 中的代码不相同。如果我查看您粘贴的代码和您截屏的代码,它们也不相同。修复此部署问题,修复评论中提到的 SnowyTracks 点,看看会发生什么。

I think something is messed up in your setup. It's not possible that the program really reaches the breakpoint return false as it did in your screenshot, since bugged is true. Furthermore, in your second screenshot you're not yet in the catch, but on the return false in the inner part of the try. This statement will not throw an exception, so if you end up in the catch, it's very odd. My guess is that the deployed code and the code in your IDE is not identical. If I look at the code you pasted and the code you made a screenshot of, they aren't identical either. Fix this deployment problem, fix the point SnowyTracks mentioned in the comment and see what happens then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文