2016-11-22 139 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); 
}