简体   繁体   English

从Restful Web服务异步访问数据

[英]Accessing data from Restful web service asynchronously

I am writing an android application where all my data is expose using web service. 我正在编写一个Android应用程序,其中所有数据都使用Web服务公开。 I am writing a wrapper class to fetch data from the services called DataManager. 我正在编写一个包装器类,以从称为DataManager的服务中获取数据。

public class DataManager{

public DataManager (){}


public String getValue ()
{
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet ("some url");

    try 
    {
        HttpResponse response = client.execute(get);
        if (response.getStatusLine ().getStatusCode() == 200)
        {
            HttpEntity entity = response.getEntity ();
            String result = EntityUtils.toString(entity);
            return result;
        }
    } 
    catch (ClientProtocolException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

} }

When I call this method a RuntimeException is generated on the UI thread. 当我调用此方法时,UI线程上会生成RuntimeException。 How can I refactor this code and fetch the values from the webservice and return these values asynchronously. 我如何重构此代码并从Web服务获取值并异步返回这些值。 [NOTE] I am writing this for some team members so they can use it to get data from the service so I want it to be has easy has possible for them to use. [注意]我正在为某些团队成员编写此文件,以便他们可以使用它来从服务中获取数据,因此我希望它可以轻松使用。

Use AsyncTask or IntentService . 使用AsyncTaskIntentService Doing networking (and in general lengthy operations) on UI thread is no-no on Android. 在Android上,在UI线程上进行联网(通常需要很长时间)。

Here you got with AsyncTask fundamentals and some tutorials And here you got with IntentService fundamentals and tutorials 在这里,您获得了AsyncTask基础知识和一些教程,并且在这里获得了IntentService基础知识教程。

I'd use AsyncTask myself (IntentService's jobs are queued), but be aware about change in the way it is handled in newer APIs . 我本人会使用AsyncTask(IntentService的作业已排队),但请注意在较新的API中处理方式的更改 Not a big deal (just a few more code lines), though 不过没什么大不了的(仅几行代码)

我建议尝试一下David Chandler的Android 基本HTTP客户端库。

Best way to do this is to use an IntentService. 最好的方法是使用IntentService。 That gives you a background thread and a way to pass it a URL. 这样就为您提供了后台线程,并为您传递了URL。 Depending on how much data you have, you can either dump it into a local file or store it in a database. 根据您拥有的数据量,您可以将其转储到本地文件中或将其存储在数据库中。

If you do the database route, you can load the data asynchronously in an Activity using AsyncTaskLoader, or do full access using AsyncTask. 如果执行数据库路由,则可以使用AsyncTaskLoader在活动中异步加载数据,或者使用AsyncTask进行完全访问。 And, with some extra overhead but much more simple coding, you can stick the data in a ContentProvider and then use a CursorLoader in your Activity. 并且,通过一些额外的开销和更简单的编码,您可以将数据粘贴到ContentProvider中,然后在Activity中使用CursorLoader。

Above all, never do network operations or database operations on the UI thread. 最重要的是,永远不要在UI线程上执行网络操作或数据库操作。 Use your Activity as a "presenter" (an intelligent "view") and put all the controller and model stuff in the background. 将您的“活动”用作“演示者”(智能“视图”),并将所有控制器和模型内容置于后台。

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

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