如何像 Java 那样存储 cookie Android?
How to store cookie Android like in Java?
我想使用我在 Java 应用程序中使用的相同 class,它在 PHP 服务器上使用 Cookie 机制。实际 class,在 Java 中效果很好:
public class 连接 {
private HttpURLConnection connection;
private String username;
private String password;
public Connection(String username, String password) {
super();
this.username = username;
this.password = password;
CookieHandler.setDefault(new CookieManager());
login();
}
public Connection() {
CookieHandler.setDefault(new CookieManager());
}
public void setCredentials(String username, String password) {
this.username = username;
this.password = password;
login();
}
public String login() {
String urlParameters = "username=" + username + "&password=" + password
+ "&ac=log";
return sendPost(
my url.php",
urlParameters);
}
public String sendPost(String destination, String post) {
try {
URL url = new URL(destination);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(post);
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
在 Java 下,我可以毫无问题地管理从服务器收到的 cookie;在 Android 中,当我执行方法 login(); 时,我获得了一个新的 PHPsession;那么如何解决这个问题呢?我只想在 Android 和 PHP 服务器之间保持与身份验证的连接。
据我所知,这个想法是存储您从服务器收到的令牌。您可以使用下面的代码将令牌保存到共享首选项中,并且每当您需要再次发出请求时,读取令牌并用它签署您的请求。
将令牌写入共享首选项:
SharedPreferences settings = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(ACCESS_TOKEN_STRING, token);
/* Commit the edits */
editor.commit();
读取令牌:
SharedPreferences settings = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
return settings.getString(ACCESS_TOKEN_STRING, "");
我想使用我在 Java 应用程序中使用的相同 class,它在 PHP 服务器上使用 Cookie 机制。实际 class,在 Java 中效果很好:
public class 连接 {
private HttpURLConnection connection;
private String username;
private String password;
public Connection(String username, String password) {
super();
this.username = username;
this.password = password;
CookieHandler.setDefault(new CookieManager());
login();
}
public Connection() {
CookieHandler.setDefault(new CookieManager());
}
public void setCredentials(String username, String password) {
this.username = username;
this.password = password;
login();
}
public String login() {
String urlParameters = "username=" + username + "&password=" + password
+ "&ac=log";
return sendPost(
my url.php",
urlParameters);
}
public String sendPost(String destination, String post) {
try {
URL url = new URL(destination);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(post);
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
在 Java 下,我可以毫无问题地管理从服务器收到的 cookie;在 Android 中,当我执行方法 login(); 时,我获得了一个新的 PHPsession;那么如何解决这个问题呢?我只想在 Android 和 PHP 服务器之间保持与身份验证的连接。
据我所知,这个想法是存储您从服务器收到的令牌。您可以使用下面的代码将令牌保存到共享首选项中,并且每当您需要再次发出请求时,读取令牌并用它签署您的请求。
将令牌写入共享首选项:
SharedPreferences settings = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(ACCESS_TOKEN_STRING, token);
/* Commit the edits */
editor.commit();
读取令牌:
SharedPreferences settings = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
return settings.getString(ACCESS_TOKEN_STRING, "");