gpt4 book ai didi

java - Retrofit 不会从 Android 将 Java 对象作为 PHP 后端的 POST 参数传递

转载 作者:行者123 更新时间:2023-12-02 01:51:18 24 4
gpt4 key购买 nike

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

这就是我现在的做法。

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

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接口(interface)

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 子级的电子邮件密码

最佳答案

像这样使用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 对象提供。

关于java - Retrofit 不会从 Android 将 Java 对象作为 PHP 后端的 POST 参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com