You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Genymotion+Android时WampServer会话丢失,Retrofit2环境下求助

Fixing WampServer Session Persistence Issue with Genymotion & Retrofit 2

Hey there, let's work through this session problem you're facing—totally makes sense that it works on your PC but breaks in Genymotion; emulators have a few quirks when talking to local backend services. Here's a breakdown of the most likely fixes:

1. Use the Correct Network Address for Genymotion

Your PC's localhost doesn't map directly to your host machine from Genymotion. Instead, you need to use Genymotion's dedicated host IP: 10.0.3.2 (this is VirtualBox's default NAT mapping for the host machine).

  • Update your Retrofit base URL from http://localhost/... to http://10.0.3.2/... (don't forget to include your Wamp port if it's not 80, e.g., http://10.0.3.2:8080/...).
  • Verify Wamp allows external access:
    1. Open Wamp's Apache config (httpd.conf).
    2. Find sections like <Directory "c:/wamp64/www/"> and ensure they have:
      AllowOverride All
      Require all granted
      
    3. If using virtual hosts, check httpd-vhosts.conf to make sure you're not restricting access only to localhost.

Wamp uses PHP sessions tied to cookies, but Retrofit doesn't automatically save or send cookies by default. You need to add a CookieJar to manage session cookies between requests.

Add the PersistentCookieJar dependency to your build.gradle (Module level):

implementation 'com.github.franmontiel:PersistentCookieJar:v1.0.1'

Then configure Retrofit to use it:

val cookieJar = PersistentCookieJar(
    SetCookieCache(),
    SharedPrefsCookiePersistor(applicationContext)
)

val okHttpClient = OkHttpClient.Builder()
    .cookieJar(cookieJar)
    .build()

val retrofit = Retrofit.Builder()
    .baseUrl("http://10.0.3.2/your-api-endpoint/")
    .client(okHttpClient)
    .addConverterFactory(GsonConverterFactory.create())
    .build()

Option 2: Custom CookieJar (No Third-Party Lib)

If you prefer not to use external libraries, implement a simple CookieJar to store cookies in memory:

class MyCookieJar : CookieJar {
    private val cookies = mutableListOf<Cookie>()

    override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
        this.cookies.addAll(cookies)
    }

    override fun loadForRequest(url: HttpUrl): List<Cookie> {
        return cookies.filter { it.matches(url) }
    }
}

// Then attach it to OkHttpClient
val okHttpClient = OkHttpClient.Builder()
    .cookieJar(MyCookieJar())
    .build()

3. Adjust Wamp's PHP Session Configuration

Your PHP session settings might be blocking the emulator's requests from maintaining sessions:

  1. Open php.ini in Wamp (right-click Wamp tray icon > PHP > php.ini).
  2. Find and modify these settings:
    • session.cookie_domain: Set this to an empty string (session.cookie_domain = "") or comment it out. If it's set to localhost, the emulator's requests (from IP 10.0.3.2) won't match the cookie domain.
    • session.cookie_secure: Set to Off (session.cookie_secure = Off). If this is On, PHP will only send cookies over HTTPS, which your local Wamp setup likely isn't using.
  3. Restart Wamp after making changes.

4. Add CORS Headers to Wamp (If Needed)

Sometimes the emulator's requests are treated as cross-origin, which can block cookie transmission. Add these headers to your .htaccess file (in your Wamp www directory) or Apache config:

Header set Access-Control-Allow-Origin "http://10.0.3.2"
Header set Access-Control-Allow-Credentials "true"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type, Authorization"

Quick Test to Isolate the Issue

Before diving into code changes, test the backend directly from Genymotion's built-in browser:

  1. Open the browser in Genymotion.
  2. Navigate to your login URL (http://10.0.3.2/login).
  3. Log in, then visit the protected endpoint that returns "log out".
    If this works, the problem is in your Retrofit cookie handling. If not, focus on Wamp's network/session config.

内容的提问来源于stack exchange,提问作者JMAR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:14:55