进行异步服务调用Android中 [英] Making Asynchronous Service Calls in Android

查看:124
本文介绍了进行异步服务调用Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我使用的AsyncTask类Android应用程序的异步服务电话,但在我的活动每一个服务呼叫我需要在我的应用程序子类,可以扩展的AsyncTask。任何人都可以建议我这样做?一个更好的办法

I am trying to make asynchronous service-call in my android app using AsyncTask Class, but for each and every service call in my activity i need to have a subclass in my Application which Extends AsyncTask. Can anyone Suggest me a better way of doing this???

在我的方案服务呼叫有不同的返回类型,那么我需要返回所有服务调用的对象,并根据其类型转换,是否有这样做的什么更好的办法?

In My Scenario Service Calls have different return-type,then i need to return an object for all service calls and typecast it according, Is there any better way of doing this ???

推荐答案

不要声明的AsyncTask作为一个内部类的活动。在它自己的范围内声明该类一次,你可以在任何地方使用它。至于数据进出,可以使第一个参数字符串或URI,并通过在请求数据,并可以在AsyncTask的使用内部侦听器对​​象可以设置该活动接收呼叫回来的时候onPostExecute火灾。

Don't declare the AsyncTask as an inner class of your activity. Declare this class once in it's own scope, and you can use it from anywhere. As far as the data to pass in and out, you can make the first parameter a String or Uri and pass in the request data, and you can use an internal listener object in the AsyncTask that can be set by the activity to receive a call back when onPostExecute fires.

修改

我做了一个依据来证明这种最简单的版本。使用示例AsyncTask的派生类,您可以重用这个类在您的整个应用程序,并把它传递的所有端点和接收结果的活动内(或其他任何地方,你需要让你的服务调用)。如果你需要让这个更广泛的,你可以改变输入对象从字符串到一些更复杂的包含数据结构,任何你需要传递给你的服务电话(后参数,可以HTTP方法等),你可以更改签名在OnPostExecuteListener接口,如果你想返回结构化数据,而不仅仅是原始结果

I made a gist to demonstrate the simplest version of this. Using the example AsyncTask-derived class, you can reuse this one class throughout your entire app and pass it all of your endpoints and receive the result inside your activities (or anywhere else you need to make your service calls). If you need to make this more extensive, you can just change the input object from a String to some more complex data structure containing anything you need to pass to your service call (post params, http method, etc) and you can change the signature of the OnPostExecuteListener interface if you want to return structured data and not just the raw result

https://gist.github.com/80c59fb70e195ca142bb

这篇关于进行异步服务调用Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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