Java android 等待来自服务器的响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19229204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:16:06  来源:igfitidea点击:

android waiting for response from server

javaandroidmultithreadingeventshttprequest

提问by Amir.F

say I want to perform an Http request from the server, this process takes time.

说我想从服务器执行一个 Http 请求,这个过程需要时间。

now because of this, the http request needs to be run on a different thread (AsyncTask, Runnable, etc.)

现在正因为如此,http 请求需要在不同的线程上运行(AsyncTask、Runnable 等)

but sometimes I just need the response when I ask for it, in order to update the UI

但有时我只需要在我要求时得到响应,以便更新 UI

using Thread.sleep in a loop to wait for the response is not good performance wise

在循环中使用 Thread.sleep 来等待响应不是很好的性能明智的

example: I want the user's name, I ask the server for it, and I have to wait for it now the activity calls the UserManager that calls the serverHandler that performs the operation and returns the result back

例如:我想要用户名,我向服务器询问它,我现在必须等待它现在活动调用 UserManager 调用执行操作的 serverHandler 并将结果返回

maybe an event system is in order, but I'm not sure how to do this in my scenerio and quite frankly I am really confused on this issue

也许一个事件系统是有序的,但我不知道如何在我的场景中做到这一点,坦率地说,我对这个问题真的很困惑

please help someone?

请帮助某人?

采纳答案by Alex Fu

This can most definitely be done w/ AsyncTask... Handle the network request in doInBackground()and once doInBackground()is finished, onPostExecute()is triggered on the UI thread and that's where you can execute any code that will update UI elements.

这绝对可以用 AsyncTask 来完成...处理网络请求doInBackground(),一旦doInBackground()完成,onPostExecute()就会在 UI 线程上触发,在那里你可以执行任何将更新 UI 元素的代码。

If you need something a bit more generic and re-usable, you would probably want to implement a callback... I'll refer to the UI thread as the clientand the AsyncTask as the server.

如果您需要更通用和可重用的东西,您可能想要实现回调...我将 UI 线程称为客户端,将 AsyncTask 称为服务器

  1. Create a new interface and create some method stubs.

    public interface MyEventListener {
        public void onEventCompleted();
        public void onEventFailed();
    } 
    
  2. Have your client pass instance of MyEventListener to the server. A typical way of doing this is to have your client implement the interface (MyEventListener) and pass itself to the server.

    public class MyActivity implement MyEventListener {
    
        public void startEvent() {
            new MyAsyncTask(this).execute();
        }           
    
        @Override
        public void onEventCompleted() {
            // TODO
        }
    
        @Override
        public void onEventFailed() {
            // TODO
        }
    }
    
  3. On the onPostExecuteof the server, check if the callback is null and call the appropriate method.

    public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
        private MyEventListener callback;
    
        public MyAsyncTask(MyEventListener cb) {
            callback = cb;
        }
    
        [...]
    
        @Override
        protected void onPostExecute(Void aVoid) {
            if(callback != null) {
                callback.onEventCompleted();
            }
        }
    }
    
  1. 创建一个新接口并创建一些方法存根。

    public interface MyEventListener {
        public void onEventCompleted();
        public void onEventFailed();
    } 
    
  2. 让您的客户端将 MyEventListener 的实例传递给服务器。执行此操作的典型方法是让您的客户端实现接口 (MyEventListener) 并将其自身传递给服务器。

    public class MyActivity implement MyEventListener {
    
        public void startEvent() {
            new MyAsyncTask(this).execute();
        }           
    
        @Override
        public void onEventCompleted() {
            // TODO
        }
    
        @Override
        public void onEventFailed() {
            // TODO
        }
    }
    
  3. onPostExecute服务器上,检查回调是否为空并调用适当的方法。

    public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
        private MyEventListener callback;
    
        public MyAsyncTask(MyEventListener cb) {
            callback = cb;
        }
    
        [...]
    
        @Override
        protected void onPostExecute(Void aVoid) {
            if(callback != null) {
                callback.onEventCompleted();
            }
        }
    }
    

You can read more about callbacks here: http://www.javaworld.com/javaworld/javatips/jw-javatip10.html

您可以在此处阅读有关回调的更多信息:http: //www.javaworld.com/javaworld/javatips/jw-javatip10.html