---
title: "为AsyncHttpClient设置Cookie"
author: "ringw"
date: "2014-03-19T12:45:42Z"
tags: []
categories: ["移动开发"]
slug: "asynchttpclient-cookie"
draft: false
lastmod: "2014-05-27T18:34:11Z"
id: 2033456
url: "https://ringw.iteye.com/blog/2033456"
---

使用AsyncHttpClient向服务端提交数据，有时需要带cookie。给AsyncHttpClient设置Cookie的方法如下：

AsyncHttpClient myClient = new AsyncHttpClient();

PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
BasicClientCookie newCookie = new BasicClientCookie("name1", "value1");
newCookie.setVersion(1);
newCookie.setDomain("mydomain.com");
newCookie.setPath("/");
myCookieStore.addCookie(newCookie);

myClient.setCookieStore(myCookieStore);

myClient.post(url, params, new AsyncHttpResponseHandler(){});


参考：
http://loopj.com/android-async-http

http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html