显示“请稍候”在网络服务调用期间
我有一个通过 .net Web 服务检查用户名可用性的活动,我需要在通话期间显示“请等待”消息,因为它可能需要一两秒钟 - 我已经尝试过 AsyncTask 和 Threading,但我遇到的问题是我实际上想要阻塞主(也是唯一)线程,直到 Web 服务调用完成,因为我依赖它的结果来处理其他逻辑。
我已经尝试了各种方法,但没有任何乐趣 - 有人可以帮忙吗?
根据要求,这是我的代码的一部分:
private OnClickListener RegistrationListener = new OnClickListener() {
public void onClick(View v) {
ClearFieldHighlighting();
if (ValidateData()) {
if (IsOnline()) {
if (UsernameIsAvailable()) {
try {
iNETUserID = CreateOnlineUser();
} catch (Exception e) {
}
..更多代码.. 这
是“UsernameIsAvailable”函数:
public boolean UsernameIsAvailable() {
Boolean bUsernameIsAvailable = false;
SoapObject request = new SoapObject(TA.sNAMESPACE,
TA.METHOD_IsUsernameAvailable);
request.addProperty("sUsername", sEmail);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
TA.WEBSERVICES_URL);
try {
androidHttpTransport.call(TA.SOAP_ACTION_IsUsernameAvailable,
envelope);
SoapObject thisresponse = (SoapObject) envelope.bodyIn;
for (int i = 0; i < thisresponse.getPropertyCount(); i++) {
Object obj = thisresponse.getProperty(i);
SoapPrimitive soapPrimitive = (SoapPrimitive) obj;
bUsernameIsAvailable = Boolean.parseBoolean(soapPrimitive
.toString());
}
} catch (Exception e) {
}
return bUsernameIsAvailable;
}
我正在使用 KSoap2,它是一个外部 jar 文件来处理 Web 服务内容,
谢谢 麦克风
I have an activity which checks username availability via .net web services, I need to show a 'please wait' message during the call as it can take a second or two - I have experimented with AsyncTask and Threading but the problem I have is I actually want to block the main (and only) thread until the web service call has completed as I am depending on the result from it to process other logic.
I have tried allsorts but no joy - can anyone help please?
As requested, here is a section of my code :
private OnClickListener RegistrationListener = new OnClickListener() {
public void onClick(View v) {
ClearFieldHighlighting();
if (ValidateData()) {
if (IsOnline()) {
if (UsernameIsAvailable()) {
try {
iNETUserID = CreateOnlineUser();
} catch (Exception e) {
}
..more code..
}
Here is the 'UsernameIsAvailable' function :
public boolean UsernameIsAvailable() {
Boolean bUsernameIsAvailable = false;
SoapObject request = new SoapObject(TA.sNAMESPACE,
TA.METHOD_IsUsernameAvailable);
request.addProperty("sUsername", sEmail);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
TA.WEBSERVICES_URL);
try {
androidHttpTransport.call(TA.SOAP_ACTION_IsUsernameAvailable,
envelope);
SoapObject thisresponse = (SoapObject) envelope.bodyIn;
for (int i = 0; i < thisresponse.getPropertyCount(); i++) {
Object obj = thisresponse.getProperty(i);
SoapPrimitive soapPrimitive = (SoapPrimitive) obj;
bUsernameIsAvailable = Boolean.parseBoolean(soapPrimitive
.toString());
}
} catch (Exception e) {
}
return bUsernameIsAvailable;
}
I am using KSoap2 which is an external jar file to handle the web service stuff
Thanks
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以按以下方式构建代码:
You can structure your code in the following way:
你永远不应该阻塞 main-ui 线程。只需显示一个对话框,其中包含有关正在发生的情况的消息。确保用户无法关闭对话框。一旦 AsyncTask 或 runnable 完成其任务,只需关闭对话框即可。
使用 对话框 setCancelable(false) 以确保用户无法关闭对话框
You should never ever ever block the main-ui thread. Just show a dialog with a message about what is going on. Make sure that the user can't close the dialog. As soon as the AsyncTask or runnable finishes its task just close the dialog.
Use Dialog setCancelable(false) to make sure the user can't close the dialog