Using Cookies across Activities when using HttpClient

后端 未结 6 1829
孤独总比滥情好
孤独总比滥情好 2020-12-04 15:43

I\'m trying a simple app to read in the HTML of a website, and tranform it to create an easily readable UI in Android (This is an exersize in learning android, not to make a

6条回答
  •  無奈伤痛
    2020-12-04 16:27

    CookieManager is used by the Java's internal HTTP client. It has nothing to do with Apache HttpClient.

    In your code you always create for each request a new instance of HttpClient and therefore a new CookieStore instance, which obviously gets garbage collected along with all cookies stored in as soon as that HttpClient instance goes out of scope.

    You should either

    (1) Re-use the same instance of HttpClient for all logically related HTTP requests and share it between all logically related threads (which is the recommended way of using Apache HttpClient)

    (2) or, at the very least, share the same instance of CookieStore between logically related threads

    (3) or, if you insist on using CookieManager to store all your cookies, create a custom CookieStore implementation backed by CookieManager

提交回复
热议问题