在Android中处理跨多个活动的PHP会话

问题描述:

我想实现服务器端使用php Joomla API为我的应用程序。用户发送登录信息和服务器进程并成功创建会话。但是,我无法在android中捕获此会话数据。我使用齐射执行帖子,但多个帖子似乎创建新的登录,应该不是这样,因为用户已经登录。我猜他们是一个问题,通过齐射发送头。任何人有这个解决方案,我将不胜感激。 注意服务器端正在工作100%。问题仅在于android。在Android中处理跨多个活动的PHP会话

protected void doLogin(){ 
    final String username = editTextUsername.getText().toString().trim(); 
    final String password = editTextPassword.getText().toString().trim(); 

    final CookieManager cookieManager = new CookieManager(new PersistentCookieStore(getApplicationContext()), CookiePolicy.ACCEPT_ORIGINAL_SERVER); 
    CookieHandler.setDefault(cookieManager); 

    RequestQueue queue = Volley.newRequestQueue(this); 
    String loginUrl  ="http://loginurl/sesslogin/"; 

    final StringRequest stringRequest = new StringRequest(Request.Method.POST, loginUrl, 
     new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       //COOKIE_JAR = cookieManager.getCookieStore().getCookies().toString(); 
       //PersistentCookieStore.getCookies(); 
       // Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show(); 
       //stringRequest.getHeaders().values() 

       Toast.makeText(getApplicationContext(), response , Toast.LENGTH_LONG).show(); 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(getApplicationContext(), "That didn't work!", Toast.LENGTH_LONG).show(); 
      } 
     } 
    ){ 
     @Override 
     protected Map<String,String> getParams(){ 
      Map<String,String> params = new HashMap<String, String>(); 
      params.put(KEY_USERNAME,username); 
      params.put(KEY_PASSWORD,password); 
      return params; 
     } 
    }; 

    queue.add(stringRequest); 
} 

我还得到了一个我在Github上发现的共享偏好和cookie管理器的实现,它是我的代码的一部分。但我没有看到这个代码的任何影响。

public class PersistentCookieStore implements CookieStore { 

/** 
* The default preferences string. 
*/ 
private final static String PREF_DEFAULT_STRING = ""; 

/** 
* The preferences name. 
*/ 
private final static String PREFS_NAME = PersistentCookieStore.class.getName(); 

/** 
* The preferences session cookie key. 
*/ 
private final static String PREF_SESSION_COOKIE = "Set-Cookie"; 

private CookieStore mStore; 
private Context mContext; 

/** 
* @param context The application context 
*/ 
public PersistentCookieStore(Context context) { 
    // prevent context leaking by getting the application context 
    mContext = context.getApplicationContext(); 

    // get the default in memory store and if there is a cookie stored in shared preferences, 
    // we added it to the cookie store 
    mStore = new CookieManager().getCookieStore(); 
    String jsonSessionCookie = getJsonSessionCookieString(); 
    if (!jsonSessionCookie.equals(PREF_DEFAULT_STRING)) { 
     Gson gson = new Gson(); 
     HttpCookie cookie = gson.fromJson(jsonSessionCookie, HttpCookie.class); 
     mStore.add(URI.create(cookie.getDomain()), cookie); 
    } 
} 

@Override 
public void add(URI uri, HttpCookie cookie) { 
    if (cookie.getName().equals("sessionid")) { 
     // if the cookie that the cookie store attempt to add is a session cookie, 
     // we remove the older cookie and save the new one in shared preferences 
     remove(URI.create(cookie.getDomain()), cookie); 
     saveSessionCookie(cookie); 
    } 

    mStore.add(URI.create(cookie.getDomain()), cookie); 
} 

@Override 
public List<HttpCookie> get(URI uri) { 
    return mStore.get(uri); 
} 

@Override 
public List<HttpCookie> getCookies() { 
    return mStore.getCookies(); 
} 

@Override 
public List<URI> getURIs() { 
    return mStore.getURIs(); 
} 

@Override 
public boolean remove(URI uri, HttpCookie cookie) { 
    return mStore.remove(uri, cookie); 
} 

@Override 
public boolean removeAll() { 
    return mStore.removeAll(); 
} 

private String getJsonSessionCookieString() { 
    return getPrefs().getString(PREF_SESSION_COOKIE, PREF_DEFAULT_STRING); 
} 

/** 
* Saves the HttpCookie to SharedPreferences as a json string. 
* 
* @param cookie The cookie to save in SharedPreferences. 
*/ 
private void saveSessionCookie(HttpCookie cookie) { 
    Gson gson = new Gson(); 
    String jsonSessionCookieString = gson.toJson(cookie); 
    SharedPreferences.Editor editor = getPrefs().edit(); 
    editor.putString(PREF_SESSION_COOKIE, jsonSessionCookieString); 
    editor.apply(); 
} 

private SharedPreferences getPrefs() { 
    return mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
} 

}

我已经确定了问题。所以我会为任何遇到同样问题的人回答这个问题。问题在这里;

final CookieManager cookieManager = new CookieManager(new PersistentCookieStore(getApplicationContext()), CookiePolicy.ACCEPT_ORIGINAL_SERVER); 
CookieHandler.setDefault(cookieManager); 

这个CookieManager出于某种原因应该在onCreate方法期间安装。此外,类型决赛在这里是不需要的。我的最终代码如下:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    //INSTANTIATE COOKIE MANAGER 
    CookieManager cookieManager = new CookieManager(new PersistentCookieStore(this.getApplicationContext()), CookiePolicy.ACCEPT_ORIGINAL_SERVER); 
    CookieHandler.setDefault(cookieManager); 
    doLogin(); 
} 
+0

你应该接受你自己的答案。 – Elin

+0

它不适用于我,任何其他提示? – aletede91