如何使用单独的线程来执行HTTP请求 [英] How to use separate thread to perform http requests

查看:168
本文介绍了如何使用单独的线程来执行HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

予有正在执行的HTTP请求(具体地调用的FogBugz API),当用户点击某些按钮的应用程序。现在,我只是创建服务应用程序启动时,然后调用不同的方法,在服务完成请求。然而,当我做到这一点,有一个在UI线程通常挂起。我看过的AsyncTask,但我不知道它会做什么,我要完成的任务。因为我需要立即解析HTTP请求的回报,我需要有一个过程,能够这些数据返回到UI线程的XML。将AsyncTask的是能够做到这一点,还是有一些其他的方式?

I have an application that is performing HTTP Requests (specifically calling the FogBugz API) when the user clicks certain buttons. Right now, I am just creating a service when the application starts, and then calling different methods in that service to complete requests. However, when I do this, there is the usual hang in the UI thread. I have looked at AsyncTask, but am not sure it will do what I want to accomplish. Because I need to instantly parse the XML that the HTTP Request returns, I need to have a process that is able to return this data to the UI thread. Will ASyncTask be able to accomplish this, or is there some other way?

    public static InputStream makeRequest(String httpRequest)
    {
        In a separate thread, run HTTP Request, get back and process, return inputstream
    }

该方法是由几个人打电话来执行的Htt prequests。一旦输入流被返回时,其它方法解析为特定的信息。

This method is called by several others to perform HttpRequests. Once the inputstream is returned, the other methods parse for specific information.

推荐答案

这样做也只是像做

//Body of your click handler
Thread thread = new Thread(new Runnable(){
  @Override
  public void run(){
    //code to do the HTTP request
  }
});
thread.start();

这将导致的run()方法执行一个新的线程来执行里面的code。你可以看看一个异步任务,如果你喜欢,尽管我个人从来没有使用过。这是一个快速和简单的方式做事情。

That will cause the code inside the run() method to execute in a new thread of execution. You can look into an async task if you like although I've personally never used it. This is a quick and simple way to get things done.

至于传递信息回来,我会用一个 Handler对象从而有效地让你建立消息队列对给定的线程和消息传递给它造成的特定code中的执行。你需要做的原因是因为Android将不允许大于UI线程以外的任何线程更新的实际UI。

With regards to passing information back, I would use a Handler object which effectively allows you to set up a message queue for a given thread and pass messages to it which cause the execution of specific code. The reason you need to do this is because Android will not let any thread other than the UI thread update the actual UI.

这是否解决您的问题吗?我知道我的第一遍没有完全解决所有的问题。

Does that address your question? I know my first pass didn't fully address all of your issues.

修改基本上,你做的是在你的活动定义处理对象喜欢

Edit Basically, what you do is define a handler object in your Activity like

private Handler handler_ = new Handler(){
    @Override
    public void handleMessage(Message msg){

    }

};

您还创建静态 INT 常数,以帮助告知处理该怎么做。你基本上使用这些,以使几个不同类型的消息被传递到一个处理程序的一个实例。如果只有将要被传递回一个消息,那么你不必担心。

You also create static int constants that help tell the handler what to do. You basically use those to allow for several different types of messages to be passed to one instance of a handler. If there is only going to be one message that is passed back, then you don't have to worry about that.

例如

private static final int UPDATE_UI = 1;

要发送消息给你打电话的处理程序

To send a message to the handler you call

handler_.sendMessage(Message.obtain(handler_, UPDATE_UI, inputStreamInstance));

从处理程序:

From the handler:

private Handler handler_ = new Handler(){
    @Override
public void handleMessage(Message msg){
    switch(msg.what){
            case UPDATE_UI:
            InputStream is = (InputStream)msg.obj;
            //do what you need to with the InputStream
            break;
        }
    }

};

另外,这里的inputStreamInstance被添加到消息的对象,你可以通过任何你喜欢这样的对象,如果你想解析成某种类型的容器对象或东西这样,你可以做到这一点为好。单从处理程序中转换回该对象。

Alternatively, where the inputStreamInstance is added to the Message object, you can pass any object you like so if you wanted to parse it into some kind of container object or something like that, you could do that as well. Just cast it back to that object from within the handler.

这篇关于如何使用单独的线程来执行HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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