Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android httpclient登录并使用cookies进行进一步处理_Android_Cookies_Httpclient - Fatal编程技术网

Android httpclient登录并使用cookies进行进一步处理

Android httpclient登录并使用cookies进行进一步处理,android,cookies,httpclient,Android,Cookies,Httpclient,我正在开发一个应用程序,其中包括登录活动和主要活动。 如果用户首次登录,应用程序将保存用户名并传入sharedPrefs。在下一次启动时,登录活动使用这些用户名和密码,前提是服务器返回true(在xml中,getEntity)主活动意图启动。登录后,我想使用启动登录中设置的cookies与网页进行交互。就我在网上搜索而言,他们说我应该使用同一个httpclient,以免丢失cookie。我试过了,但还是不行。那么,我可以使用cookies而不使用相同的httpclient吗 我的应用程序的一般逻

我正在开发一个应用程序,其中包括登录活动和主要活动。 如果用户首次登录,应用程序将保存用户名并传入sharedPrefs。在下一次启动时,登录活动使用这些用户名和密码,前提是服务器返回true(在xml中,getEntity)主活动意图启动。登录后,我想使用启动登录中设置的cookies与网页进行交互。就我在网上搜索而言,他们说我应该使用同一个httpclient,以免丢失cookie。我试过了,但还是不行。那么,我可以使用cookies而不使用相同的httpclient吗

我的应用程序的一般逻辑:

httpclient.execute("http://www.abc.com/index.php?process=login&user="+variable1+"&pass="+variable1); 

//here I get the entity of this response and I parse that return value, after that, if(login==true)--> go on...
//here I have to read all page from website which is protected by user authentication(by cookies).(ex:index.php?process=getmymessages)
//But I did not manage that. At this point what is your suggestions?

提前谢谢

> p>您可以考虑通过像这样的单一解决方案使用同一个HTTPclipse:

public enum MyAppHttpClient {
    INSTANCE;

    private HttpClient configuredHttpClient = null;

    public HttpClient getConfiguredHttpClient() {
        if (configuredHttpClient == null) {
            try {
                HttpParams params = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(params, 5000);
                HttpConnectionParams.setSoTimeout(params, 5000);
                configuredHttpClient = new DefaultHttpClient(params);
            } catch (Exception e) {
                configuredHttpClient = new DefaultHttpClient();
            }
        }

        return configuredHttpClient;
    }
}
您可以在任何需要MyAppHttpClient.INSTANCE.GetConfigureDhtpClient()的地方调用它

如果还不够,您可以自己管理cookies,BasicCookieStore类是一个很好的起点,您可以检查以下线程:


希望对您有所帮助。

如何使用“MyAppHttpClient.INSTANCE.GetConfigureDhtpClient()”定义新的HttpClient?我尝试了这种方法,但没有成功:DefaultHttpClient htpost=MyAppHttpClient.INSTANCE.getConfiguredHttpClient();在try{}块之间,“configuredHttpClient”赋值在哪里?它保持空值,您可以再次更新代码吗?您可以这样使用:HttpClient HttpClient=ClaimHttpClient.INSTANCE.getHttpClient();HttpGet HttpGet=新的HttpGet(someUrl);HttpResponse response=httpclient.execute(httpget);抱歉,我已更新代码,try{}块的最后一行已更改为:configuredHttpClient=new DefaultHttpClient(params);谢谢。它起作用了。但是,如果我尝试执行新的httpget,就会出现错误。错误为:SingleClientConnManager(1402):SingleClientConnManager的使用无效:连接仍已分配。我怎样才能避免这种情况发生?