Android HttpClient persistent cookies

后端 未结 4 1035
眼角桃花
眼角桃花 2020-11-30 02:10

UPDATE: This question and its answers should no longer be recommended to anyone reading this. Android no-longer recommends HttpClient (read: deprecated), and instead

相关标签:
4条回答
  • 2020-11-30 02:15

    Make your httpClient a singleton class.

    0 讨论(0)
  • 2020-11-30 02:23

    Don't create new HttpClients; this will clear the cookies. Reuse a single HttpClient.

    0 讨论(0)
  • 2020-11-30 02:23

    define HttpClient in Application class, and use it in activity.

    in Application

    public class AAA extends Application {
        public HttpClient httpClient; 
    
        httpClient = new DefaultHttpClient(); 
    

    in Activity

    AAA aaa = (AAA)getApplication();
    httpClient = app.httpClient;
    
    0 讨论(0)
  • 2020-11-30 02:30

    You can do what @Emmanuel suggested or you can pass the BasicHttpContext between the HttpClients you are creating.

    Example Use of context and cookies, complete code here

        HttpClient httpclient = new DefaultHttpClient();
    
        // Create a local instance of cookie store
        CookieStore cookieStore = new BasicCookieStore();
    
        // Create local HTTP context
        HttpContext localContext = new BasicHttpContext();
        // Bind custom cookie store to the local context
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    
        HttpGet httpget = new HttpGet("http://www.google.com/", localContext);
    
    0 讨论(0)
提交回复
热议问题