Android 4.4 KitKat未接收cookie
在我的应用程序中,我向服务器发送POST请求,并从服务器接收响应。 从回复中我收集了不同的cookies,特别是用户信息。因此,我发送一个登录请求,并在服务器响应后接收cookies,以保持登录。在安卓4.3及以下版本中,我收到的cookies很好,用户登录成功。但在安卓4.4中,用户成功登录,但没有收到cookie 安卓系统是否改变了一些重要的事情,让这一切发生了?如果有人有任何建议,下面是我的代码Android 4.4 KitKat未接收cookie,android,cookies,android-4.4-kitkat,Android,Cookies,Android 4.4 Kitkat,在我的应用程序中,我向服务器发送POST请求,并从服务器接收响应。 从回复中我收集了不同的cookies,特别是用户信息。因此,我发送一个登录请求,并在服务器响应后接收cookies,以保持登录。在安卓4.3及以下版本中,我收到的cookies很好,用户登录成功。但在安卓4.4中,用户成功登录,但没有收到cookie 安卓系统是否改变了一些重要的事情,让这一切发生了?如果有人有任何建议,下面是我的代码 private URL urlObj; private HttpURLConnection c
private URL urlObj;
private HttpURLConnection connection;
private DataOutputStream dataOs;
private ArrayList<String> schools;
private ArrayList<Post> schoolPosts;
private String schoolID;
private String name;
private String userLoginCookie, sessionSeedCookie, sessionUidCookie, sPrefCookie;
private Context context;
private CookieStore store;
public DataParser(Context _context) {
context = _context;
}
//First call whenever connecting across the user's network
private void establishConnection() throws IOException {
urlObj = new URL(url);
connection = (HttpURLConnection) urlObj.openConnection();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
store = cookieManager.getCookieStore();
getCookies();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Cookie", sessionSeedCookie+";"+sessionUidCookie+";"+userLoginCookie+";"+sPrefCookie);
connection.setDoOutput(true);
connection.setUseCaches(false);
dataOs = new DataOutputStream(connection.getOutputStream());
}
//Called after communication is complete
private void disconnectAll() throws IOException {
connection.disconnect();
dataOs.close();
}
private void getCookies() {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
userLoginCookie = settings.getString(USER_LOGIN, BLANK);
Log.d(LOG, "Cookie: "+userLoginCookie);
sessionSeedCookie = settings.getString(SESS_SEED, BLANK);
Log.d(LOG, "Cookie: "+sessionSeedCookie);
sessionUidCookie = settings.getString(SESS_UID, BLANK);
Log.d(LOG, "Cookie: "+sessionUidCookie);
sPrefCookie = settings.getString(S_PREF, "sPref="+BLANK);
Log.d(LOG, "Cookie: "+sPrefCookie);
}
private void updateCookies() {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
List<HttpCookie> cookieList = store.getCookies();
for(int i=0; i<cookieList.size(); i++) {
if(cookieList.get(i).getName().equals(USER_LOGIN))
editor.putString(USER_LOGIN, cookieList.get(i).toString());
else if(cookieList.get(i).getName().equals(SESS_SEED))
editor.putString(SESS_SEED, cookieList.get(i).toString());
else if(cookieList.get(i).getName().equals(SESS_UID))
editor.putString(SESS_UID, cookieList.get(i).toString());
else
Log.d(LOG, "Found Extra Cookie: "+cookieList.get(i).getName());
}
sPrefCookie = settings.getString(S_PREF, "sPref="+BLANK);
editor.commit(); //Save changes to the SharedPreferences
}
//Logins User into Walkntrade
public String login(String email, String password) throws IOException {
establishConnection(); //Instantiate all streams and opens the connection
String query= "intent=login&password="+password+"&email="+email+"&rememberMe=true";
dataOs.writeBytes(query);
Log.d(LOG, "" + connection.getResponseCode());
updateCookies();
String response = readInputAsString(connection.getInputStream());
Log.d(LOG, "Connection Status: "+response);
disconnectAll();
return response;
}
//Logs user out of Walkntrade
public void logout() throws IOException {
establishConnection();
String query = "intent=logout";
dataOs.writeBytes(query);
Log.d(LOG, "" + connection.getResponseCode());
updateCookies();
disconnectAll();
}
//Returns user login status
public static boolean isUserLoggedIn(Context _context) {
SharedPreferences settings = _context.getSharedPreferences(PREFS_NAME, 0);
boolean isUserLoggedIn = settings.getBoolean(DataParser.CURRENTLY_LOGGED_IN, false);
return isUserLoggedIn;
}
public String getUserName() throws IOException{
establishConnection();
String query = "intent=getUserName";
dataOs.writeBytes(query);
Log.d(LOG, ""+connection.getResponseCode());
updateCookies();
String response = readInputAsString(connection.getInputStream());
disconnectAll();
return response;
}
public String getUserAvatar() throws IOException {
establishConnection();
String query = "intent=getAvatar";
dataOs.writeBytes(query);
Log.d(LOG, ""+connection.getResponseCode());
updateCookies();
String response = readInputAsString(connection.getInputStream());
disconnectAll();
return response;
}
私有URL urlObj;
私有HttpURLConnection;
私有数据输出流数据操作系统;
私立ArrayList学校;
私立ArrayList学校;
私人字符串学校ID;
私有字符串名称;
私有字符串userLoginCookie、sessionSeedCookie、sessionUidCookie、sPrefCookie;
私人语境;
私人库克商店;
公共数据解析器(上下文_上下文){
上下文=_上下文;
}
//在通过用户网络连接时第一次呼叫
私有连接()引发IOException{
urlObj=新URL(URL);
connection=(HttpURLConnection)urlObj.openConnection();
CookieManager CookieManager=新建CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
setDefault(cookieManager);
store=cookieManager.getCookieStore();
getCookies();
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
setRequestProperty(“Cookie”,sessionSeedCookie+”;“+sessionUidCookie+”;“+userLoginCookie+”;“+sPrefCookie”);
connection.setDoOutput(真);
connection.setUseCaches(false);
dataOs=新的DataOutputStream(connection.getOutputStream());
}
//通信完成后调用
私有void disconnectAll()引发IOException{
连接断开();
dataOs.close();
}
私有void getCookies(){
SharedReferences设置=context.getSharedReferences(首选项名称,0);
userLoginCookie=settings.getString(用户登录,空白);
Log.d(Log,“Cookie:+userLoginCookie);
sessionSeedCookie=settings.getString(SESS\u SEED,空白);
Log.d(Log,“Cookie:+sessionSeedCookie);
sessionUidCookie=settings.getString(SESS\u UID,空白);
Log.d(Log,“Cookie:+sessionUidCookie);
sPrefCookie=settings.getString(S_PREF,“sPref=“+BLANK”);
Log.d(Log,“Cookie:+sPrefCookie);
}
私有void updateCookies(){
SharedReferences设置=context.getSharedReferences(首选项名称,0);
SharedReferences.Editor=settings.edit();
List cookieList=store.getCookies();
对于(int i=0;i我修复了这个问题。我从HttpUrlConnection和其他java.net内容切换到AndroidHttpClient和其他Apache Http类。现在可以从Android API 19检索cookie
[编辑]
我使用了AndroidHttpClient(),并遵循了一些Apache Http教程。因此,在对代码稍作修改后,看起来如下所示:
private AndroidHttpClient httpClient; //Android Client, Uses User-Agent, and executes request
private HttpContext httpContext; //Contains CookieStore that is sent along with request
private CookieStore cookieStore; //Holds cookies from server
private HttpPost httpPost; //Contains message to be sent to client
private final String USER_AGENT = System.getProperty("http.agent"); //Unique User-Agent of current device
//First call whenever connecting across the user's network
private void establishConnection() {
cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); //Attach CookieStore to the HttpContext
getCookies(); //Retrieve currently stored cookies
httpClient = AndroidHttpClient.newInstance(USER_AGENT);
httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("Cookie", sessionSeedCookie + ";" + sessionUidCookie + ";" + userLoginCookie + ";" + sPrefCookie);
}
//Called after communication is complete
private void disconnectAll() {
httpClient.close();
}
//Sends out POST request and returns an InputStream
private InputStream processRequest(String query) throws IOException{
httpPost.setEntity(new StringEntity(query)); //wraps the query into a String entity
HttpResponse response = httpClient.execute(httpPost, httpContext); //Executes the request along with the cookie store
Log.i(TAG, "Server Response Code: " + response.getStatusLine().getStatusCode()); //Reads response code from server
updateCookies();
return response.getEntity().getContent();
}
//Logins User into Walkntrade
public String login(String email, String password) throws IOException {
establishConnection(); //Instantiate all streams and opens the connection
String query= "intent=login&password="+password+"&email="+email+"&rememberMe=true";
InputStream inputStream = processRequest(query);
String serverResponse = readInputAsString(inputStream); //Reads message response from server
disconnectAll();
return serverResponse;
}
在KitKat 4.4更新之前,这段代码对我来说运行良好-
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//handle cookies
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
它在4.4.2之后坏了(至少我当时注意到了),并且不再收到cookies。只需在打开urlConnection之前移动CookieManager和CookieMhandler,就可以再次修复它。奇怪的是,它以前工作过
//handle cookies
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
你能详细说明一下吗?自从4.4坏了我的电脑后,我就遇到了这个问题Cookies@WallyHale我添加了用于新实现的实际代码。我还注意到使用Apache客户端的速度有所提高。