android等待服务器的响应 [英] android waiting for response from server

查看:150
本文介绍了android等待服务器的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想要从服务器执行一个Http请求,这个过程需要时间。因为这个原因,http请求需要运行在另一个不同的位置上。



线程(AsyncTask,Runnable等)



但有时我只需要响应,为了更新UI



在循环中使用Thread.sleep等待响应不太好性能明智



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



也许一个事件系统是有序的,但我不知道如何在我的scenerio
这样做,坦率地说,我真的很困惑在这个问题上



请帮助某人?

解决方案

这可以绝对完成w / AsyncTask ...处理 doInBackground()一旦 doInBackground()完成, onPostExecute()在UI线程上触发,那就是您可以执行任何将更新UI元素的代码。



如果你需要一些更通用和可重用的东西,你可能需要实现一个回调...我将引用UI线程作为客户端和AscentTask作为服务器


  1. 创建一个新界面并创建一些方法存根。

      public interface MyEventListener {
    public void onEventCompleted();
    public void onEventFailed();
    }


  2. 让您的客户端将MyEventListener的实例传递到服务器。一个典型的方法是让客户端实现接口(MyEventListener)并将其自身传递给服务器。

      public class MyActivity实现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();
    }
    }
    }


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


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

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

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

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

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

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?

解决方案

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.

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 client and the AsyncTask as the server.

  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 onPostExecute of 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();
            }
        }
    }
    

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

这篇关于android等待服务器的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆