2016-11-22 45 views
0

由于我可以通过改进的post方法发送对象,我需要通过将他的电话和密码发送到以数组接收数据的文件来验证用户如何通过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(
+0

欢迎来到Stack Overflow!请查看我们的[SO问题清单](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)来帮助你提出一个好问题,从而得到一个很好的答案。 –

回答

1
数据

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

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