如何使用Retrofit通过post方法发送对象

时间:2016-11-22 21:44:53

标签: android retrofit slim

由于我可以通过post方法通过改造发送对象,我需要通过将他的手机和密码发送到以数组形式接收数据的文件来验证用户

我有以下代码块

private void makeRetrofitCall(String phone, String password)
{
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(ApiInterface.URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RequestPost requestPost = new RequestPost(phone,password);

    ApiInterface interfaces = retrofit.create(ApiInterface.class);

    Call<ResponsePost> responsePostCall =   interfaces.AuthUser(requestPost);

    responsePostCall.enqueue(new Callback<ResponsePost>() {
        @Override
        public void onResponse(Call<ResponsePost> call,   Response<ResponsePost> response) {
            if(response.isSuccessful())
            {
                ResponsePost rp=response.body();
                Log.d("Respondido", rp.toString());
            }
            Log.d("ERROR>>>>>>>>>>>>>",response.message());
        }

        @Override
        public void onFailure(Call<ResponsePost> call, Throwable t) {
        }
    });
}

我的2级

public class RequestPost {
@SerializedName("Phone")
private String Phone;
@SerializedName("Password")
private String Password;

public RequestPost()
{
}

public RequestPost(String phone, String password)
{
    this.Phone = phone;
    this.Password = password;
}

public String getPhone() {
    return Phone;
}

public void setPhone(String phone) {
    this.Phone = phone;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    this.Password = password;
}
}

public class ResponsePost {
@SerializedName("Name")
private String Name;
@SerializedName("LastName")
private String LastName;
@SerializedName("Email")
private String Email;
@SerializedName("Phone")
private String Phone;

检索信息的回复帖子

public ResponsePost()
{
}

public ResponsePost(String nombre, String apellido, String mail,String telefono)
{
    this.Name = nombre;
    this.LastName = apellido;
    this.Email = mail;
    this.Phone = telefono;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getLastName() {
    return LastName;
}

public void setLastName(String lastName) {
    LastName = lastName;
}

public String getEmail() {
    return Email;
}

public void setEmail(String email) {
    Email = email;
}

public String getPhone() {
    return Phone;
}

public void setPhone(String phone) {
    Phone = phone;
}

@Override
public String toString()
{
    return "ResponsePost{" +
            "Name ='" + Name + '\'' +
            ", LastName ='" + LastName + '\'' +
            ", Email =" + Email +'\''+
            ", Phone =" + Phone +
            '}';
}
}

这是我在SLIM 3中的路线,它应该接收邮寄

发送的数据
$this->post('login', function ($req, $res) {
    $um = new UserModel();      

    return $res
        ->withHeader('Content-type', 'application/json')
        ->getBody()
        ->write(
            json_encode(
                $um->Login(
                    $req->getParsedBody()
                )
            )
        );
});

我应该如何定义我的端点ApiInterfface

public interface ApiInterface {

String URL = "http://edwineds.com/moviles/public/mobi/";
@FormUrlEncoded
@POST("login")
     Call<ResponsePost> AuthUser(@Body RequestPost user);
}

错误Logcat android

    java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (parameter #1)
                                                                        for method ApiInterface.AuthUser
                                                                        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:667)
                                                                        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:658)
                                                                        at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:676)
                                                                        at retrofit2.ServiceMethod$Builder.parseParameterAnnotation(ServiceMethod.java:616)
                                                                        at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:328)
                                                                        at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:201)
                                                                        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
                                                                        at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
                                                                        at java.lang.reflect.Proxy.invoke(Proxy.java)
                                                                        at $Proxy0.AuthUser(Unknown Source)
                                                                        at com.example.edwin.taxi.Login.makeRetrofitCall(Login.java:129)
                                                                        at com.example.edwin.taxi.Login.access$300(Login.java:25)
                                                                        at com.example.edwin.taxi.Login$2.onClick(Login.java:56)
                                                                        at android.view.View.performClick(View.java)
                                                                        at android.view.View$PerformClick.run(View.java)
                                                                        at android.os.Handler.handleCallback(Handler.java)
                                                                        at android.os.Handler.dispatchMessage(Handler.java)
                                                                        at android.os.Looper.loop(Looper.java)
                                                                        at android.app.ActivityThread.main(ActivityThread.java)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(

1 个答案:

答案 0 :(得分:1)

由于您将请求正文作为JSON发送,因此不应使用@FormUrlEncoded注释。 @Body注释就足够了。

public interface ApiInterface {
    String URL = "http://edwineds.com/moviles/public/mobi/";
    @POST("login")
    Call<ResponsePost> AuthUser(@Body RequestPost user);
}
相关问题