我无法理解奇怪的 Android AsyncTask bug
因此,我使用 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返回它。但在某个地方一切都出了问题,我不知道该怎么办:/!!
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 :/!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我要注意的一件事是,您应该使用 .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 :)
我认为你的设置有些混乱。程序不可能像屏幕截图中那样真正到达断点
return false
,因为bugged
为true
。此外,在第二个屏幕截图中,您尚未进入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, sincebugged
istrue
. Furthermore, in your second screenshot you're not yet in thecatch
, but on thereturn 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.