Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
改型不会在Android的PHP后端将Java对象作为POST参数提供_Java_Php_Android_Retrofit_Retrofit2 - Fatal编程技术网

改型不会在Android的PHP后端将Java对象作为POST参数提供

改型不会在Android的PHP后端将Java对象作为POST参数提供,java,php,android,retrofit,retrofit2,Java,Php,Android,Retrofit,Retrofit2,使用改型,我想将Java对象的值作为参数发布到后端的PHP脚本中。但是使用下面的代码,我无法接收$\u POST变量中的任何参数 下面是我现在是如何做到这一点的 1)这就是我如何构建和获取改装实例的方法 public class RetrofitClient { private static Retrofit retrofit; private static final String BASE_URL = "http://www.example.com"; publi

使用改型,我想将Java对象的值作为参数发布到后端的PHP脚本中。但是使用下面的代码,我无法接收
$\u 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;
    }
}
public interface RequestAPI {

    @POST("/login")
    Call<ResponseBody> getResponse(@Body LoginModel loginModel) ;
}
@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);
        }
    });
}
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 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;
    }
}
public interface RequestAPI {

    @POST("/login")
    Call<ResponseBody> getResponse(@Body LoginModel loginModel) ;
}
@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);
        }
    });
}
现在每次我转储$\u POST变量时。它只给出一个空数组

我错过了什么


为什么我作为$\u POST的孩子没有收到电子邮件密码

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

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

由于此替代方法直接将整个原始请求正文作为变量提供,而不是将其作为要编码的JSON对象提供。

对于API方法,请这样使用:

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

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

由于此替代方法直接将整个原始请求正文作为变量提供,而不是将其作为要编码的JSON对象提供。

是否检查了响应。isSuccessful()?是否尝试使用
@FormUrlEncoded
数据?看起来无法通过
echo json\u encode($\u POST)访问原始请求正文
@jeelvankhee我试过使用
@FormUrlEncoded
,这很有效,但不允许Java对象作为@Body传递。@Radesh是的,它给出的是True是否检查了响应。isSuccessful()?您试过
@FormUrlEncoded
数据吗?看起来无法通过
echo json\u encode($\u POST)访问原始请求正文
@jeelvankhee我试过使用
@FormUrlEncoded
,这是可行的,但不允许Java对象作为@Body传递。@Radesh是的,它给出了trueI,我已经这样做了。但这并不能解决我的问题,因为我希望传递Java对象作为参数。在我的问题中,只有一个示例。我会在更复杂的场景中使用它。然后您需要更改PHP端的接收器代码,因为很明显,您不能直接以
json\u编码的
格式接收自定义的原始正文。检查以下答案:非常感谢您的解决方案。另外,请用您提供的信息更新您的答案,以便我可以接受答案。因此,它可以帮助任何其他开发者有相同的问题。@ KANDUO请考虑编辑作为您的方便,我刚刚提供了一瞥作为编辑。我已经做到了。但这并不能解决我的问题,因为我希望传递Java对象作为参数。在我的问题中,只有一个示例。我会在更复杂的场景中使用它。然后您需要更改PHP端的接收器代码,因为很明显,您不能直接以
json\u编码的
格式接收自定义的原始正文。检查以下答案:非常感谢您的解决方案。另外,请用您提供的信息更新您的答案,以便我可以接受答案。因此,它可以帮助任何其他开发者有相同的问题。@ KANDUO请考虑编辑作为您的方便,我刚刚提供了一瞥作为编辑。