本文介绍了Android的 - HttpClient的为backgroundservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,登录到Web服务,并上传文件。我需要保持会话活着,我去不同的屏幕,并从Web服务获取数据。我读了我需要的HTTP调用作为一种服务,也许我的引导与服务应用程序。我怎么把我的登录活动和上传活动HttpClient的一个HTTP服务活动里面调用?

i have an app that login to a webservice and also uploads file.I need to keep the session alive as i go to different screens and get data from webservice.I read i need to make the http calls as a service and maybe boot my app with the service.How do i put my "login" activity and "upload" activity httpclient calls inside a http service activity?

感谢。

推荐答案

由于服务在同一个线程UI线程上运行,你需要在不同的线程中运行的服务。您可以通过几种不同的方式做到这一点:

Since a service runs on the same thread as the UI thread, you will need to run the service in a different thread. You can do this in several different ways:

  1. 内使用普通的Java线程该服务的的onCreate() onBind()等方法
  2. 使用的AsyncTask的的onCreate()方法中 - 线程的另一种形式,但如果你需要做的UI更新更清洁
  3. 使用 IntentService 提供异步服务执行任务 - 不知道如何好这个工作,因为我从来没有使用过
  1. Use regular java threading within the service's onCreate () or onBind() etc, methods
  2. Use AsyncTask within the onCreate() method - another form of threading but much cleaner if you need to do UI updates
  3. Use IntentService which provides asynchronous Service task execution - not sure how well this works as I have never used it.

这三种方法应该允许你进行连接与背景,并通过服务,即使我从来没有使用IntentService HttpClient的,它看起来像我的最佳选择。如果您需要进行修改,只能在UI线程来完成用户界面的AsyncTask将是非常有益的。

All three of these methods should allow you to make connections with the HttpClient in the background and through a Service and even though I have never used IntentService, it looks like the best option to me. AsyncTask would be very useful if you need to make changes to the UI which can only be done on the UI thread.

编辑根据要求:所以我现在做的事情,需要的Http以异步方式连接。让这个帖子之后,我试着做3号和它的工作非常好/容易。唯一的问题是,信息必须通过的意图是十分可怕的两个上下文之间传递。因此,这里是东西,你可以做,使HTTP连接在异步,后台,服务近似的例子。

Edit by request: So I am currently doing something which requires Http connections in a asynchronous way. After making this post, I tried doing number 3 and it does work very well/easily. The only problem is that information has to be passed between two contexts through intents which is really ugly. So here is an approximate example of something you can do to make http connections in an asynchronous, background, service.

从外部活动启动异步服务。我把两个按钮只是这样的活动可以看出,服务运行时执行。这样做的目的可以发射真正的任何地方,你觉得像。

Launch the asynchronous service from an outside activity. I put two buttons just so the activity can be seen executing while the service is running. The intent can be launched really anywhere you feel like.

/* Can be executed when button is clicked, activity is launched, etc.
   Here I launch it from a OnClickListener of a button. Not really relevant to our interests.                       */
public void onClick(View v) {
        Intent i = new Intent ("com.test.services.BackgroundConnectionService");
        v.getContext().startService(i);         
    }

然后在 BackgroundConnectionService 您必须扩展IntentService类,并实现在 onHandleIntent(意向意图)方法。它是那么容易,因为这个例子:

Then within the BackgroundConnectionService you have to extend the IntentService class and implement all http calls within the onHandleIntent(Intent intent) method. It is as easy as this example:

public class BackgroundConnectionService extends IntentService {

    public BackgroundConnectionService() {
        // Need this to name the service
        super ("ConnectionServices");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        // Do stuff that you want to happen asynchronously here
        DefaultHttpClient httpclient = new DefaultHttpClient ();
        HttpGet httpget = new HttpGet ("http://www.google.com");
        // Some try and catch that I am leaving out
        httpclient.execute (httpget);
    }
}

最后,声明异步服务,你会在AndroidManifest.xml任何正常业务范围内文件中的<应用> 标签

...
        <service android:name="com.test.services.BackgroundConnectionService">
            <intent-filter>
                <action android:name="com.test.services.BackgroundConnectionService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
...

这应约做到这一点。它实际上是pretty的方便:D

That should about do it. It is actually pretty easy : D

这篇关于Android的 - HttpClient的为backgroundservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:05