首页 > 解决方案 > Retrofit 不会在 Android 的 PHP 后端将 Java 对象作为 POST 参数传递

问题描述

使用 Retrofit 我想将 Java 对象的值作为参数发布到后端的 PHP 脚本。但是使用下面的代码我无法在$_POST变量中接收任何参数。

这就是我现在的做法。

1)这就是我构建和获取改造实例的方式

public class RetrofitClient {

    private static Retrofit retrofit;
    private static final String BASE_URL = "http://www.example.com";

    public static Retrofit getInstance() {
        if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

2) 发送参数的模型

public class LoginModel {

    @SerializedName("email")
    private String email;

    @SerializedName("password")
    private String password;

    public LoginModel(String email, String password) {
        this.email = email;
        this.password = password;
    }

    public String getEmail() { return email; }
    public String getPassword() { return password; }
}

3) API 接口

public interface RequestAPI {

    @POST("/login")
    Call<ResponseBody> getResponse(@Body LoginModel loginModel) ;
}

4)这就是我触发请求并捕获响应的方式

@Override
public void onClick(View v) {

    LoginModel loginModel = new LoginModel("user@example.com", "123");

    RequestAPI service = RetrofitClient.getInstance().create(RequestAPI.class);

    Call<ResponseBody> call = service.getResponse(loginModel);

    call.enqueue(new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                Log.e("=====", response.body().string());
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("=====", "error", t);
        }
    });
}

5)后端的PHP脚本

echo json_encode($_POST);

现在每次我转储 $_POST 变量。它只是给出一个空数组

我有什么遗漏吗。

为什么我没有收到$_POST 的孩子的电子邮件密码?

标签: javaphpandroidretrofitretrofit2

解决方案


像这样使用API 方法

@FormUrlEncoded
@POST("/login")
Call<ResponseBody> getResponse(@Field("email") String email, @Field("password") String password) ;

& 像这样调用:

@Override
public void onClick(View v) {

LoginModel loginModel = new LoginModel("user@example.com", "123");

RequestAPI service = RetrofitClient.getInstance().create(RequestAPI.class);

Call<ResponseBody> call = service.getResponse(loginModel.getEmail(), loginModel.getPassword());

call.enqueue(new Callback<ResponseBody>() {

    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        try {
            Log.e("=====", response.body().string());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("=====", "error", t);
    }
});
}

编辑:

如果您仍想将自定义对象作为 API 调用的原始主体传递,那么PHP端代码将会发生变化:

而不是使用

echo json_encode($_POST);

考虑使用

$post_body = file_get_contents('php://input');

因为这种替代方法直接将整个原始请求主体作为变量提供,而不是将其作为要编码的JSON 对象提供。


推荐阅读