使用Genymotion+Android时WampServer会话丢失,Retrofit2环境下求助
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/...tohttp://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:
- Open Wamp's Apache config (
httpd.conf). - Find sections like
<Directory "c:/wamp64/www/">and ensure they have:AllowOverride All Require all granted - If using virtual hosts, check
httpd-vhosts.confto make sure you're not restricting access only tolocalhost.
- Open Wamp's Apache config (
2. Enable Cookie Persistence in Retrofit
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.
Option 1: Use a Persistent Cookie Library
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:
- Open
php.iniin Wamp (right-click Wamp tray icon > PHP > php.ini). - 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 tolocalhost, the emulator's requests (from IP 10.0.3.2) won't match the cookie domain.session.cookie_secure: Set toOff(session.cookie_secure = Off). If this isOn, PHP will only send cookies over HTTPS, which your local Wamp setup likely isn't using.
- 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:
- Open the browser in Genymotion.
- Navigate to your login URL (
http://10.0.3.2/login). - 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

