HttpPost cookies持久化android

HttpPost cookies持久化android,android,http-post,httpclient,cookiestore,Android,Http Post,Httpclient,Cookiestore,我想在post请求后保存cookies。用于httpClient实现的类: public class JSONParser { CookieStore store = new BasicCookieStore(); static InputStream is = null; static JSONObject jObj = null; static String json = ""; private static final

我想在post请求后保存cookies。用于httpClient实现的类:

 public class JSONParser {
    CookieStore store = new BasicCookieStore();
       static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
        private static final DefaultHttpClient httpClient = new DefaultHttpClient() ;
     public static DefaultHttpClient getInstance() { return httpClient; }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            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);

            DefaultHttpClient httpClient = JSONParser.getInstance();
            List<Cookie> cookies = httpClient.getCookieStore().getCookies();
            httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Android-AEApp,ID=2435743");
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            httpPost.setHeader("User-Agent","Android-AEApp,ID=2435743");

            cookies = httpClient.getCookieStore().getCookies();
            httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            httpPost.setHeader("Cookie", "PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2; path=/");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            cookies = httpClient.getCookieStore().getCookies();

            if (cookies.isEmpty()) {
                for (Cookie a : cookies)
                cookieStore.addCookie(a);
            } else {
                for (Cookie a : cookies) {
                    cookieStore.addCookie(a);
                    System.out.println("- " + a.getValue().toString());
                }
            }

            is = httpEntity.getContent();


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
       }
我没有饼干。当我不包括localContext时,我可以使用

 if (cookies.isEmpty()) {
                for (Cookie a : cookies)
                cookieStore.addCookie(a);
            } else {
                for (Cookie a : cookies) {
                    cookieStore.addCookie(a);
                    System.out.println("- " + a.getValue().toString());
                }
            }

但cookies不会在第二次请求时进行设置。我猜-原因在于不使用localContext。告诉我问题出在哪里,如果cookie一直在更改,我如何设置cookie?

我使用Singletone作为cookie的ArrayList。看起来是这样的:

 HttpResponse httpResponse = httpClient.execute(httpPost, localContext);
public class CookieStorage {


    private ArrayList<Object> arrayList;

    private static CookieStorage instance;

    private CookieStorage(){
        arrayList = new ArrayList<Object>();
    }

    public static CookieStorage getInstance(){
        if (instance == null){
            instance = new CookieStorage();
        }
        return instance;
    }

    public ArrayList<Object> getArrayList() {
        return arrayList;
    }

    @Override
    public String toString()
    {       
    return getArrayList().toString();   
    }
}
public class CookieStorage{
私有ArrayList ArrayList;
私有静态CookieStorage实例;
私人厨师店{
arrayList=新的arrayList();
}
公共静态CookieStorage getInstance(){
if(实例==null){
实例=新CookieStorage();
}
返回实例;
}
公共ArrayList getArrayList(){
返回数组列表;
}
@凌驾
公共字符串toString()
{       
返回getArrayList().toString();
}
}
和我的HttpClient类:

public class HttpClient {
static InputStream is = null;
private static final DefaultHttpClient httpClient = new DefaultHttpClient() ;
public static DefaultHttpClient getInstance() { return httpClient; }

 public static InputStream getResponse(String url, List<NameValuePair> params)
 {
    try {
    CookieStore cookieStore = new BasicCookieStore();
    DefaultHttpClient httpClient = JSONParser.getInstance();        
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Android-AEApp,ID=2435743");
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
    BasicClientCookie cookie = new BasicClientCookie("PHPSESSID", "1");  
    httpClient.setCookieStore(cookieStore);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params));
    httpPost.setHeader("User-Agent","Android-AEApp,ID=2435743");
    httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    System.out.println(CookieStorage.getInstance().toString());
    if (CookieStorage.getInstance().getArrayList().isEmpty())
        CookieStorage.getInstance().getArrayList().add("PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2");
    httpPost.setHeader("Cookie", CookieStorage.getInstance().getArrayList().get(0).toString());
    HttpResponse httpResponse = httpClient.execute(httpPost);
    Header[] head = httpResponse.getAllHeaders();
    System.out.println(cookie);

    if (httpResponse.getLastHeader("Set-Cookie")!=null)
    {

        CookieStorage.getInstance().getArrayList().remove(0);
        CookieStorage.getInstance().getArrayList().add(httpResponse.getLastHeader("Set-Cookie").getValue());
    }
    Log.i("arrayList",(CookieStorage.getInstance().getArrayList().toString()));

    HttpEntity httpEntity = httpResponse.getEntity();
    is = httpEntity.getContent();


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

     return is;
 }


}
公共类HttpClient{
静态InputStream为空;
私有静态最终DefaultHttpClient httpClient=新的DefaultHttpClient();
公共静态DefaultHttpClient getInstance(){return httpClient;}
公共静态InputStream getResponse(字符串url,列表参数)
{
试一试{
CookieStore CookieStore=新的BasicCookieStore();
DefaultHttpClient httpClient=JSONParser.getInstance();
httpClient.getParams().setParameter(CoreProtocolPNames.USER_代理,“Android AEApp,ID=2435743”);
httpClient.getParams().setParameter(ClientPNames.COOKIE_策略,CookiePolicy.RFC_2109);
BasicClientCookie=新的BasicClientCookie(“PHPSESSID”,“1”);
httpClient.setCookieStore(cookieStore);
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
setHeader(“用户代理”,“安卓AEApp,ID=2435743”);
setHeader(“Accept”,“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”);
System.out.println(CookieStorage.getInstance().toString());
if(CookieStorage.getInstance().getArrayList().isEmpty())
CookieStorage.getInstance().getArrayList().add(“PHPSESSID=lc89a2uuu0rj6t2p29219gc2cq4i2”);
setHeader(“Cookie”,CookieStorage.getInstance().getArrayList().get(0.toString());
HttpResponse HttpResponse=httpClient.execute(httpPost);
Header[]head=httpResponse.getAllHeaders();
System.out.println(cookie);
if(httpResponse.getLastHeader(“设置Cookie”)!=null)
{
CookieStorage.getInstance().getArrayList().remove(0);
CookieStorage.getInstance().getArrayList().add(httpResponse.getLastHeader(“设置Cookie”).getValue());
}
Log.i(“arrayList”,(CookieStorage.getInstance().getArrayList().toString());
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
回报是;
}
}