简体   繁体   English

Java / Android-如何使用处理程序设置超时外部服务调用

[英]Java/Android - How to set timeout external service call with handler

My scenario is an onCreate() activity method which executes the following code (simplified): 我的场景是一个onCreate()活动方法,该方法执行以下代码(简化):

dialog.show(); //loading wheel
try {
    remote.sendRequest(myData, new MyHandler());
}
catch (Exception e) {
    dialog.dismiss();
    //log and react    
}

class MyHandler extends SDKSpecificCompiledHandler {

    @Override
    public void failure() {
        dialog.dismiss();
        //do stuff
    }

    @override
    public void success() {
        dialog.dismiss();
        //do stuff
    }

}

//I have read-only access to this handler!
public abstract class SDKSpecificCompiledHandler {
    public abstract void success(JSONObject successData);
    public abstract void failure(JSONObject errorData);
}

Explanation: A remote service is called passing an handler that gets called when he's done. 说明:一个远程服务被称为传递处理程序,该处理程序完成后将被调用。 A loading wheel (dialog) is shown to the user until a success, failure or exception happens. 向用户显示装载轮(对话),直到成功,失败或异常发生。

The problem is when the service gets successfully called but no response ever comes. 问题是当服务被成功调用但没有响应时。 In that case dialog.dismiss() doesn't get called and the loading wheel keeps spinning for ever. 在这种情况下,不会调用dialog.dismiss() ,并且加载轮永远旋转。

What I need is a sort of timeout which dismisses the dialog (and possibly takes other actions) after some seconds if the server doesn't get back. 我需要的是一种超时机制,如果服务器不恢复运行,它将在几秒钟后关闭对话框(并可能采取其他措施)。

My first though would be to create a new thread for the service call, and right after the launch set a timer which dismisses the dialog. 我的第一个操作是为服务调用创建一个新线程,并在启动后立即设置一个计时器来关闭对话框。 Would it be a good idea? 这是个好主意吗?

Thank you, 谢谢,

EDIT : 编辑

The service is third-party/not editable. 该服务是第三方/不可编辑的。 I'm using a pre-compiled artifact. 我正在使用预编译的工件。

It is better to use time out in service call itself, You can set the time out with service , If you need know how to set the time out then I should know what kind of service you are using ? 最好在服务调用本身中使用超时,您可以通过服务设置超时,如果您需要知道如何设置超时,那么我应该知道您正在使用哪种服务?

One more thing is that if you are using a loader you should make that loader in such a way that it can be cancel by the client. 还有一件事是,如果您使用的是加载程序,则应以一种可以被客户端取消的方式制作该加载程序。

Still not really sure what you're trying to achieve but if you want to run some code after some time on main thread (ie your code will do stuff to the UI), you can use a android.os.Handler 仍然不确定自己要实现什么,但是如果您想在主线程上运行一段时间后再运行一些代码(即您的代码将对UI起作用),则可以使用android.os.Handler

    mHandler = new Handler(getMainLooper());
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // do stuff on UI thread
        }
    },10000);

When your call returned from the server, simply cancel the messages on the queue: 从服务器返回呼叫时,只需取消队列中的消息即可:

    mHandler.removeCallbacksAndMessages(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM