需要我的应用程序登录并保持相同的会话ID,直到我注销。在mij登录我用这个:
public class loginTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String loginData = params[0];
String text = "";
BufferedReader reader = null;
// Send data
try {
// Defined URL where to send data
URL url = new URL(URL);
// getting cookies:
URLConnection conn = url.openConnection();
conn.connect();
// setting cookies
cookieManager.setCookies(url.openConnection());
cookieManager.storeCookies(conn);
}
当应用程序自动登录时,它需要将一些数据发送到不同的URL:
Public class sendVisumNo extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String visumNo = params[0];
String text = "";
BufferedReader reader = null;
// Send data
try {
// Defined URL where to send data
URL send_visum_url= new URL(SEND_VISUM_URL);
// getting cookies:
conn = loginURL.openConnection();
conn.connect();
// setting cookies
cookieManager.setCookies(loginURL.openConnection());
cookieManager.storeCookies(conn);
}
如何为SEND_VISUM_URL提供与第一个相同的Cookie /会话ID?直到我退出
非常感谢任何帮助!
答案 0 :(得分:0)
您可以使用SharedPreferences
来执行此操作
This is more complete answer to shared preferences.
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
preferences = getSharedPreferences("com.example.yourpackagename.bla", Context.MODE_PRIVATE);`
存储数据:
String cookies="someweirdbigstring";
editor = preferences.edit();
editor.putString("cookies",cookies);
editor.apply()
检索数据:
String cookies = preferences.getString("cookies", null);
if (cookies!=null){
//Do stuff with cookies
}else{
// No cookies
}