You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android应用对接本地服务器失败:Retrofit示例请求无响应

Hey there, let's work through this Retrofit crash you're hitting when switching to your local server. I’ve debugged tons of these issues before, so let’s break down the most likely causes and fixes step by step:

First: Grab the full crash log

You mentioned the error starts with /com.journal..., but the full stack trace in Android Studio’s Logcat (filter for AndroidRuntime) will tell us exactly what’s breaking. For example, it might be an SSL error, JSON parsing mismatch, or missing permission. That’s the first thing to check—copy the full error message if you can.

1. Double-check network permissions & cleartext traffic rules

Make sure your AndroidManifest.xml has the required permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

If your local server uses HTTP (not HTTPS) and you’re targeting Android 9+ (API 28+), you’ll need to allow cleartext traffic. Add this to your AndroidManifest.xml’s <application> tag:

android:usesCleartextTraffic="true"

This is a super common gotcha when switching from a public HTTPS API to a local HTTP server.

2. Fix your Base URL & server protocol

  • Check the protocol: Most local dev servers (like XAMPP, WAMP, or Laravel’s built-in server) use HTTP by default, not HTTPS. If you’re using https://10.0.2.2, your app will throw an SSL handshake error because the server doesn’t have a valid SSL certificate. Switch to http://10.0.2.2 instead.
  • Don’t forget the port: If your server runs on a non-default port (like 8080, 8888, or 3000), add it to the Base URL. For example: http://10.0.2.2:8080/
  • Trailing slash matters: Make sure your Base URL ends with a / so Retrofit correctly combines it with your @GET path (e.g., @GET("api.php") will become http://10.0.2.2:8080/api.php).

3. Verify your ServerResponse matches the API’s JSON output

Retrofit crashes hard if your model class (ServerResponse.java) doesn’t exactly match the JSON returned by your api.php. For example:
If your api.php returns:

{
  "status": "success",
  "message": "Data fetched",
  "data": ["item1", "item2"]
}

Your ServerResponse must have matching fields (use @SerializedName if your Java field names differ):

public class ServerResponse {
    @SerializedName("status")
    private String status;
    @SerializedName("message")
    private String message;
    @SerializedName("data")
    private List<String> data;

    // Add getters and setters here
}

A mismatch here will throw a JsonSyntaxException or IllegalStateException in your log.

4. Confirm your Retrofit instance is set up correctly

Make sure you’re adding a converter factory to handle JSON parsing. If you’re using Gson (the most common choice), your Retrofit builder should look like this:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://10.0.2.2:8080/")
    .addConverterFactory(GsonConverterFactory.create()) // Don't skip this!
    .build();

Without a converter factory, Retrofit can’t parse the server’s response, which will cause an immediate crash.

5. Debug if your server is actually receiving the request

Add a log to your api.php to confirm the request is hitting the server:

// At the top of api.php
file_put_contents('request_log.txt', date('Y-m-d H:i:s') . ' - GET request received' . PHP_EOL, FILE_APPEND);

Check if request_log.txt is created in your server’s root folder. If it’s not, your Base URL or @GET path is wrong. If it is, then the issue is with the server’s response (e.g., invalid JSON, empty output).

If you can share the full stack trace from Logcat, I can narrow this down even further!


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

火山引擎 最新活动