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

使用Android Retrofit发送请求无响应问题(服务端已接收请求)

Fix: Retrofit POST Request onResponse Not Triggering (Even When Server Receives the Request)

Hey there, let's dig into why your onResponse method isn't running even though your Laravel server is correctly receiving the request body. After looking over your code and the server's response, these are the most common issues and fixes to try:

1. Don't Forget the Internet Permission

First things first—did you add the internet permission to your AndroidManifest.xml? Without this, Retrofit can't make network calls at all, and failures might happen silently:

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

This is such an easy thing to overlook, so double-check this first.

2. Fix Gson Serialization/Deserialization Issues

Your ServerJsonObject class is missing a couple of key things that Gson needs to parse the server's JSON response correctly:

  • A no-argument constructor: Gson requires this to create an instance of your class.
  • (Optional but recommended) @SerializedName annotations: These ensure Gson maps the JSON keys to your class fields correctly, even if you rename fields later.

Update your ServerJsonObject class like this:

import com.google.gson.annotations.SerializedName;

public class ServerJsonObject {
    @SerializedName("name")
    private String name;
    @SerializedName("age")
    private int age;

    // Required no-arg constructor for Gson
    public ServerJsonObject() {}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Without the no-arg constructor, Gson can't instantiate your class, which will cause parsing to fail—and Retrofit won't trigger onResponse in that case.

3. Add Logging to onFailure to Debug

Your current onFailure method is empty, so you're missing critical error information. Add logging here to see exactly why the request is failing:

@Override
public void onFailure(Call<ServerJsonObject> call, Throwable t) {
    Log.e("RetrofitError", "Request failed", t); // Prints the full exception stack trace
}

This will show you if it's a parsing error, network issue, or something else entirely.

4. Check the Server's HTTP Status Code

Make sure your Laravel server is returning a 2xx success status code (like 200). If it returns a 4xx or 5xx error, Retrofit will treat it as a failure and send it to onFailure instead of onResponse.

You can also add logging to onResponse to verify the status code:

@Override
public void onResponse(Call<ServerJsonObject> call, Response<ServerJsonObject> response) {
    Log.d("ResponseStatus", "Status code: " + response.code());
    if (response.isSuccessful()) {
        ServerJsonObject x = response.body();
        Log.d("data", Integer.toString(x.getAge()));
    } else {
        // Handle non-success responses
        try {
            Log.e("ResponseError", "Error body: " + response.errorBody().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5. Test Gson Parsing Separately

To rule out parsing issues, test Gson directly with your server's response:

String testJson = "{\"name\":\"Mathew\",\"age\":22}";
ServerJsonObject testObj = new Gson().fromJson(testJson, ServerJsonObject.class);
Log.d("GsonTest", "Parsed: name=" + testObj.getName() + ", age=" + testObj.getAge());

If this prints correctly, then Gson is working, and the issue is likely with your Retrofit setup or network.

Final Notes

The most probable culprits here are either the missing no-arg constructor in your model class or the missing internet permission. Start with those two fixes, and use the logging steps to narrow down any other issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:40:29