最好的方法ansynchronously在Android中发送HTTP GET请求? [英] Best way to send HTTP GET requests ansynchronously in Android?

查看:149
本文介绍了最好的方法ansynchronously在Android中发送HTTP GET请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Android应用程序,我不得不做出多个GET请求一个URL,以将数据传输到外部服务器(这是怎样的第三方API的工作)。

数据来自于零星。我存储这些数据在队列中,并希望将其异步,而不会减慢主UI线程发送到后台服务器。在队列中的数据的每个单元需要的GET请求。队列中有即使应用程序关闭被清空。

什么是做到这一点的最佳做法?请上传/指引我到code /教程。

解决方案
  

什么是做到这一点的最佳做法?

这将取决于什么这是何这项工作正在做。

如果这个是异步工作方式,您将使用线程以某种形式或方式:

  • 如果您的HTTP操作开着的用户界面,您可以使用的AsyncTask ,这样你就可以安全地从 onPostExecute更新UI()

  • 如果您的HTTP操作纯粹是在后台,而你想要做一次,使用 IntentService ,它有自己的后台线程工作队列

  • 如果您的HTTP操作是在后台纯粹的,你想要做逐一的时间,你都在关注确保设备应该保持清醒,而这一切是怎么回事,请考虑我的 WakefulIntentService

  • 如果您的HTTP操作纯粹是在后台,但你觉得你想要做的几个在同一时间,推出自己服务使用的执行程序用自己的线程池,确保您关机,当工作完成后该服务(如 IntentService 一样) ,并确保该设备保持清醒与 WakeLock (或许还有 WifiLock

如果这个是HTTP GET请求,使用:

  • 的HttpURLConnection

  • 的HttpClient

  • OkHttp (各地那些有额外的好处的包装),或

  • 改造(如果你的GET请求是真正的Web服务调用),或

  • 排球(如果你喜欢你的HTTP包装code是无证,不支持,但曲棍球)

  • 任意数量的其他第三方封装库

如果这个是排队,使用队列类,或的LinkedBlockingQueue 如果你计划在有多线程与它同时工作。

如果这别的东西,我帮不了你,因为我已经厌倦了猜测。

In an Android app, I have to make multiple GET requests to a URL in order to transmit data to an external server (that's how the third party API works).

Data comes in sporadically. I store this data in a queue and want to send it to the server in a background asynchronously without slowing down the main UI thread. Each unit of data in the queue requires a GET request. The queue has to be emptied even if the app closes.

What's the best practice to do this? Please post/direct me to code/tutorials.

解决方案

What's the best practice to do this?

That would depend on what "this" is and where this work is being done.

If "this" is "asynchronous work", you will use threads in one form or fashion:

  • If your HTTP operations are driving a UI, you might use AsyncTask, so you can update the UI safely from onPostExecute()

  • If your HTTP operations are purely in the background, and you want to do one at a time, use an IntentService, which has its own background thread and work queue

  • If your HTTP operations are purely in the background, and you want to do one at at time, and you are concerned about ensuring that the device should stay awake while all this is going on, consider my WakefulIntentService

  • If your HTTP operations are purely in the background, but you feel that you want to do several at a time, roll your own Service that uses an Executor with your own thread pool, making sure that you shut down that service when the work is done (as IntentService does), and making sure that the device stays awake with a WakeLock (and perhaps a WifiLock)

  • Etc.

If "this" is "HTTP GET" requests, use:

  • HttpUrlConnection, or

  • HttpClient, or

  • OkHttp (wrapper around those with added benefits), or

  • Retrofit (if your GET requests are really Web service calls), or

  • Volley (if you like your HTTP wrapper code to be undocumented, unsupported, but Googly)

  • Any number of other third-party wrapper libraries

If "this" is "queue", use the Queue class, or LinkedBlockingQueue if you plan on having multiple threads work with it at once.

If "this" is something else, I can't help you, as I'm tired of guessing.

这篇关于最好的方法ansynchronously在Android中发送HTTP GET请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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