机器人,权威性,保持HTTP连接,使用很少活动的连接 [英] android, auth, keep HTTP connection, use connection in few Activity

查看:95
本文介绍了机器人,权威性,保持HTTP连接,使用很少活动的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我试图让访问一些Web服务。我将通过基于表单的身份验证和获得授权的连接。如果我通过了授权,我想打开新的活动,但DefaultHttpClient的任何新的实例了未经授权的权利。

in my app i try to get access to some web service. I'm going through form based authentication and getting an authorized connection. If i passed authorization i want open new Activity, but any new instance of DefaultHttpClient got unauthorized right.

和问题是:结果
1)如何通过活动结果此连接
2)或如何保持正确授权的连接,如果我想HttpClient的拦截器,我在正确的道路?如果是的话,第一个问题依然存在,但传球的CookieStore数据的相关性。

And questions is:
1) How pass this connection through activity
2) Or how keep connection authorized correctly, if i think about httpclient interceptor, I'm on the right way? If, yes, then first question remain, but relativity of pass CookieStore data.

推荐答案

您应该差不多的总是作出这样的处理您的HTTP请求调用类的的ConnectionManager 这就是最常见的名称。
你应该用Singleton设计模式做出来。这样,你的连接将被正确处理。

You should almost always make a class that handles your Http Requests call is ConnectionManager thats the most common name. And you should make it with Singleton Design Pattern. This way your connections will be handled correctly.

public class ConnectionManager {
    private static ConnectionManager instance = null;
private DefaultHttpClient client;

    private ConnectionManager() {
        client = new DefaultHttpClient(...);
    }
    //public method that will be invoked from other classes.
    public static ConnectionManager getInstance() {
        if(instance == null) {
        instance = new ConnectionManager();
        }
       return instance;
    }

    public void authenticate(){
 // Do your auth call with the client here
}

    public void postStuff(){
 // Use the same client here, this way you keep using the same client for ALL of your calls.
}
}

当您需要使用的ConnectionManager使用:

When you need to use the ConnectionManager use this:

private static ConnectionManager conn = ConnectionManager.getInstance();
conn.authenticate();
conn.postStuff();

这篇关于机器人,权威性,保持HTTP连接,使用很少活动的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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