简而言之,我正在获取PHPSESSID,并将其存储起来,但是当我要加载它时,它告诉我没有cookie。我知道我需要将此phpsession ID存储在cookiestore中,以便可以在我的整个应用程序中使用它。我应该将其保存在SQLite之类的其他地方吗?
PostJson
类
/**
* Created by mohammadghali on 9/22/18.
*/
import android.content.SharedPreferences;
import android.icu.text.MessageFormat;
import android.os.AsyncTask;
import android.os.Debug;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Cookie;
public class PostJson extends AsyncTask<String , Void ,String> {
String server_response;
public OnPostTaskDoneListener mListener;
URL url;
static final String COOKIES_HEADER = "Set-Cookie";
boolean alreadyLoggedIn = false;
@Override
protected String doInBackground(String... strings) {
try{
if(strings[0].toLowerCase().equals("true")){
alreadyLoggedIn = true;
}
}catch (Exception err){
return null;
}
this.alreadyLoggedIn = alreadyLoggedIn;
Log.i("ALREADY LOGGED?: ", alreadyLoggedIn+"");
HttpURLConnection urlConnection = null;
try {
url = new URL(strings[1]);
Log.i("URL POST: ", strings[1]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
CookieManager msCookieManager = new CookieManager();
CookieHandler.setDefault(msCookieManager);
if(strings.length>2)
AddParams(urlConnection, getParamsFromString(strings[2]));
try {
if (msCookieManager.getCookieStore().get(url.toURI()).size() > 0) {
for (HttpCookie cookie : msCookieManager.getCookieStore().getCookies()) {
Log.i("LOADING COOKIE: ", cookie.getName() + ": " + cookie.getValue());
}
// While joining the Cookies, use ',' or ';' as needed. Most of the servers are using ';'
urlConnection.setRequestProperty("Cookie",
TextUtils.join(";", msCookieManager.getCookieStore().getCookies()));
} else {
Log.i("LOADING COOKIE: ", "No cookies were found");
}
}catch (Exception err){
err.printStackTrace();
}
int responseCode = urlConnection.getResponseCode();
Log.i("RESPONSE CODE POST: ", responseCode+"");
if(responseCode == HttpURLConnection.HTTP_OK){
server_response = readStream(urlConnection.getInputStream());
Log.v("CatalogClient", server_response);
//Save the cookies
Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if (cookiesHeader != null) {
if (!alreadyLoggedIn) {
msCookieManager.getCookieStore().removeAll();
Log.i("COOKIE: ", "Cleared all cookies");
}
for (String cookie : cookiesHeader) {
Log.i("COOKIE: ", cookie);
if (!alreadyLoggedIn) {
try {
msCookieManager.getCookieStore().add(url.toURI(), HttpCookie.parse(cookie).get(0));
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
}
for(HttpCookie cookie : msCookieManager.getCookieStore().getCookies()){
Log.i("STORED COOKIE: ", cookie.getName() +": "+cookie.getValue());
}
}else{
Log.e("failed", "FAILED! Error code: "+responseCode);
return "FAILED! Error code: "+responseCode;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return server_response;
//return "This is the response";
}
@Override
protected void onPostExecute(String s) {
Log.i("POST: ", "EXECUTING ON POST EXEC: "+s);
super.onPostExecute(s);
Log.e("Response", "" + server_response);
mListener.onPostTaskDone(s);
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}
public String getParamsFromMap(Map<String, String> params) {
String paramsAsString = "";
for(String key: params.keySet()){
paramsAsString+=key+"@:@"+params.get(key)+"@-@";//This will be used to separate the stings later so it has to be somehow unique
}
Log.i("Params", paramsAsString);
return paramsAsString;
}
public Map getParamsFromString(String paramsAsString){
Map<String, String> map = new HashMap<String, String>();
try{
for(String parameter : paramsAsString.split("@-@")){
map.put(parameter.split("@:@")[0], parameter.split("@:@")[1]);
}
return map;
}catch (Exception err){
Log.e("Failed", "Failed to convert params: "+err.getMessage());
return null;
}
}
private String getPostDataString(Map<String, String> params) throws UnsupportedEncodingException{
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
protected HttpURLConnection AddParams(HttpURLConnection urlConnection, Map<String, String> params) throws IOException {
OutputStream os = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(params));
writer.flush();
writer.close();
os.close();
return urlConnection;
}
public interface OnPostTaskDoneListener {
void onPostTaskDone(String responseData);
void onError();
}
}
我登录并获得了会话ID,但无法加载它。
这是日志:
12-02 12:14:08.472 19151-19151/? I/adghali.petsap: Not late-enabling -Xcheck:jni (already on)
12-02 12:14:08.537 19151-19151/? W/adghali.petsap: Unexpected CPU variant for X86 using defaults: x86
12-02 12:14:08.802 19151-19151/com.example.mohammadghali.petsapp I/adghali.petsap: The ClassLoaderContext is a special shared library.
12-02 12:14:09.122 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: JIT profile information will not be recorded: profile file does not exits.
12-02 12:14:09.124 19151-19151/com.example.mohammadghali.petsapp I/chatty: uid=10088(com.example.mohammadghali.petsapp) identical 6 lines
12-02 12:14:09.124 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: JIT profile information will not be recorded: profile file does not exits.
12-02 12:14:09.125 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: JIT profile information will not be recorded: profile file does not exits.
12-02 12:14:09.125 19151-19151/com.example.mohammadghali.petsapp I/chatty: uid=10088(com.example.mohammadghali.petsapp) identical 1 line
12-02 12:14:09.125 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: JIT profile information will not be recorded: profile file does not exits.
12-02 12:14:09.215 19151-19151/com.example.mohammadghali.petsapp I/InstantRun: starting instant run server: is main process
12-02 12:14:09.572 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
12-02 12:14:09.573 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
12-02 12:14:09.829 19151-19151/com.example.mohammadghali.petsapp D/OpenGLRenderer: Skia GL Pipeline
12-02 12:14:10.000 19151-19172/com.example.mohammadghali.petsapp I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
12-02 12:14:10.002 19151-19172/com.example.mohammadghali.petsapp I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
12-02 12:14:10.002 19151-19172/com.example.mohammadghali.petsapp I/OpenGLRenderer: Initialized EGL, version 1.4
12-02 12:14:10.003 19151-19172/com.example.mohammadghali.petsapp D/OpenGLRenderer: Swap behavior 1
12-02 12:14:10.003 19151-19172/com.example.mohammadghali.petsapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
12-02 12:14:10.003 19151-19172/com.example.mohammadghali.petsapp D/OpenGLRenderer: Swap behavior 0
12-02 12:14:10.007 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglCreateContext: 0xdf605480: maj 3 min 0 rcv 3
12-02 12:14:10.009 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:10.208 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:10.371 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:10.412 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:10.449 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:10.963 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:13.418 19151-19151/com.example.mohammadghali.petsapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
12-02 12:14:13.421 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, reflection)
12-02 12:14:13.421 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, reflection)
12-02 12:14:13.421 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (light greylist, reflection)
12-02 12:14:13.433 19151-19151/com.example.mohammadghali.petsapp W/adghali.petsap: Accessing hidden field Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe; (light greylist, reflection)
12-02 12:14:13.512 19151-19151/com.example.mohammadghali.petsapp W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@5071712
12-02 12:14:13.592 19151-19189/com.example.mohammadghali.petsapp I/ALREADY LOGGED?:: false
12-02 12:14:13.592 19151-19189/com.example.mohammadghali.petsapp I/URL POST:: http://www.pitchkings.net/pets/Services/signout.php
12-02 12:14:13.595 19151-19189/com.example.mohammadghali.petsapp I/LOADING COOKIE:: No cookies were found
12-02 12:14:13.781 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:13.827 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:13.832 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:13.837 19151-19172/com.example.mohammadghali.petsapp I/chatty: uid=10088(com.example.mohammadghali.petsapp) RenderThread identical 1 line
12-02 12:14:13.856 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:13.911 19151-19186/com.example.mohammadghali.petsapp W/adghali.petsap: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setAlpnProtocols([B)V (light greylist, reflection)
12-02 12:14:14.276 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:14.373 19151-19186/com.example.mohammadghali.petsapp W/adghali.petsap: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (light greylist, reflection)
12-02 12:14:14.378 19151-19189/com.example.mohammadghali.petsapp I/RESPONSE CODE POST:: 200
12-02 12:14:14.384 19151-19189/com.example.mohammadghali.petsapp V/CatalogClient: Logged out successfully
12-02 12:14:14.386 19151-19189/com.example.mohammadghali.petsapp I/COOKIE:: Cleared all cookies
12-02 12:14:14.386 19151-19189/com.example.mohammadghali.petsapp I/COOKIE:: PHPSESSID=91e9786974f3381f286fd443d513c50f; path=/
12-02 12:14:14.387 19151-19189/com.example.mohammadghali.petsapp I/STORED COOKIE:: PHPSESSID: 91e9786974f3381f286fd443d513c50f
12-02 12:14:14.387 19151-19151/com.example.mohammadghali.petsapp I/POST:: EXECUTING ON POST EXEC: Logged out successfully
12-02 12:14:14.387 19151-19151/com.example.mohammadghali.petsapp E/Response: Logged out successfully
12-02 12:14:14.449 19151-19151/com.example.mohammadghali.petsapp W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@6ace48e
12-02 12:14:14.527 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:14.631 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:14.655 19151-19172/com.example.mohammadghali.petsapp I/chatty: uid=10088(com.example.mohammadghali.petsapp) RenderThread identical 1 line
12-02 12:14:14.662 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:14.687 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:14.854 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:16.787 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:23.860 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:25.196 19151-19151/com.example.mohammadghali.petsapp I/AssistStructure: Flattened final assist data: 7256 bytes, containing 1 windows, 30 views
12-02 12:14:29.526 19151-19151/com.example.mohammadghali.petsapp I/AssistStructure: Flattened final assist data: 7368 bytes, containing 1 windows, 30 views
12-02 12:14:34.765 19151-19151/com.example.mohammadghali.petsapp I/Params: password@:@iamjamesbond@-@email@:@jamesbond@gmail.com@-@
12-02 12:14:34.767 19151-19205/com.example.mohammadghali.petsapp I/ALREADY LOGGED?:: false
12-02 12:14:34.767 19151-19205/com.example.mohammadghali.petsapp I/URL POST:: http://www.pitchkings.net/pets/Services/signin.php
12-02 12:14:34.816 19151-19205/com.example.mohammadghali.petsapp I/LOADING COOKIE:: No cookies were found
12-02 12:14:34.822 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:35.229 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:35.402 19151-19205/com.example.mohammadghali.petsapp I/RESPONSE CODE POST:: 200
12-02 12:14:35.403 19151-19205/com.example.mohammadghali.petsapp V/CatalogClient: {"token":"6d6542ccef1b068199c4bacdfaf65d8e"}
12-02 12:14:35.404 19151-19151/com.example.mohammadghali.petsapp I/POST:: EXECUTING ON POST EXEC: {"token":"6d6542ccef1b068199c4bacdfaf65d8e"}
12-02 12:14:35.404 19151-19151/com.example.mohammadghali.petsapp E/Response: {"token":"6d6542ccef1b068199c4bacdfaf65d8e"}
12-02 12:14:35.404 19151-19151/com.example.mohammadghali.petsapp I/Token:: 6d6542ccef1b068199c4bacdfaf65d8e
12-02 12:14:35.540 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:36.674 19151-19151/com.example.mohammadghali.petsapp W/Glide: Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
12-02 12:14:36.783 19151-19221/com.example.mohammadghali.petsapp W/ExifInterface: Stop reading file since a wrong offset may cause an infinite loop: 0
12-02 12:14:36.783 19151-19221/com.example.mohammadghali.petsapp W/ExifInterface: Stop reading file since a wrong offset may cause an infinite loop: 0
12-02 12:14:36.795 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:36.808 19151-19221/com.example.mohammadghali.petsapp W/ExifInterface: Stop reading file since a wrong offset may cause an infinite loop: 0
12-02 12:14:36.833 19151-19221/com.example.mohammadghali.petsapp I/chatty: uid=10088(com.example.mohammadghali.petsapp) glide-disk-cach identical 2 lines
12-02 12:14:36.833 19151-19221/com.example.mohammadghali.petsapp W/ExifInterface: Stop reading file since a wrong offset may cause an infinite loop: 0
12-02 12:14:36.985 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:14:37.006 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:15:14.590 19151-19151/com.example.mohammadghali.petsapp I/AssistStructure: Flattened final assist data: 7256 bytes, containing 1 windows, 30 views
12-02 12:15:14.598 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:15:14.608 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:15:19.434 19151-19151/com.example.mohammadghali.petsapp I/AssistStructure: Flattened final assist data: 7320 bytes, containing 1 windows, 30 views
12-02 12:15:21.389 19151-19151/com.example.mohammadghali.petsapp I/Params: password@:@sdf@-@email@:@asdasd@-@
12-02 12:15:21.389 19151-19239/com.example.mohammadghali.petsapp I/ALREADY LOGGED?:: true
12-02 12:15:21.389 19151-19239/com.example.mohammadghali.petsapp I/URL POST:: http://www.pitchkings.net/pets/Services/signin.php
12-02 12:15:21.421 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:15:21.437 19151-19239/com.example.mohammadghali.petsapp I/LOADING COOKIE:: No cookies were found
12-02 12:15:21.876 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:15:21.909 19151-19239/com.example.mohammadghali.petsapp I/RESPONSE CODE POST:: 200
12-02 12:15:21.910 19151-19239/com.example.mohammadghali.petsapp I/COOKIE:: PHPSESSID=1ddd47b94d4eb04526f151ff02ade5b9; path=/
12-02 12:15:21.910 19151-19151/com.example.mohammadghali.petsapp I/POST:: EXECUTING ON POST EXEC:
12-02 12:15:21.958 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
12-02 12:15:22.367 19151-19172/com.example.mohammadghali.petsapp D/EGL_emulation: eglMakeCurrent: 0xdf605480: ver 3 0 (tinfo 0xdf603710)
任何帮助将不胜感激:) 欢呼声