2017-01-02 62 views
1

我試圖做一個職位與翻新檢查是否有一些數據存在於服務器中。我的問題是,無論如何都要通過onResponse或OnFailure。代碼從未傳入這兩種方法。我在日誌中沒有出現任何錯誤。如果我調試,代碼到此爲止:call.enqueue(新的回調(),並在下一步走法外Post方法與改造永不觸發在OnResponse或在OnFailure

一些幫助將欣賞

代碼:

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); 

     Call<User> call = apiService.smsPincodeCheck(getPhoneNuber, email); 
     call.enqueue(new Callback<User>() { 
      @Override 
      public void onResponse(Call<User> call, Response<User> response) { 
       if(response.isSuccess()){ 
        Toast.makeText(RegisterThreeActivity.this, "200", Toast.LENGTH_SHORT).show(); 
       } 

       if(response.code() == 401){ 
        Toast.makeText(RegisterThreeActivity.this, "401", Toast.LENGTH_SHORT).show(); 
       } 
      } 

      @Override 
      public void onFailure(Call<User> call, Throwable t) { 

      } 
     }); 

接口:

@FormUrlEncoded 
    @POST("api/check") 
    Call<User> smsPincodeCheck(@Field("email") String email, @Field("phoneNumber") String phoneNumber); 

用戶模型:

public class User implements Serializable { 
    private final static String TAG = "User"; 

    @SerializedName("user") 
    @Expose 
    private Long id; 
    private Long commerceId; 
    private String email; 
    private String name; 
    private String lastname; 
    private String dni; 
    private String companyCharge; 
. 
. 
. Getters and setters 

JSON:

這是我試圖發送到服務器的JSON:

{ 
    "phoneNumber" : "444444444", 
    "email" : "[email protected]" 
} 

編輯1:

Call<ResponseSMS> call = apiService.smsPincodeCheck(getPhoneNuber, email); 
    call.enqueue(new Callback<ResponseSMS>() { 
     @Override 
     public void onResponse(Call<ResponseSMS> call, Response<ResponseSMS> response) { 
      if(response.isSuccess()){ 
       Toast.makeText(RegisterThreeActivity.this, "200", Toast.LENGTH_SHORT).show(); 
      } 

      if(response.code() == 401){ 
       Toast.makeText(RegisterThreeActivity.this, "401", Toast.LENGTH_SHORT).show(); 
      } 
     } 

     @Override 
     public void onFailure(Call<ResponseSMS> call, Throwable t) { 
      Toast.makeText(RegisterThreeActivity.this, "Failure", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

接口:

Call<ResponseSMS> smsPincodeCheck(@Field("email") String email, @Field("phoneNumber") String phoneNumber); 

型號:

現在0
public class ResponseSMS { 
    public String response; 
    public int status; 

    public String getResponse() { 
     return response; 
    } 

    public void setResponse(String response) { 
     this.response = response; 
    } 

    public int getStatus() { 
     return status; 
    } 

    public void setStatus(int status) { 
     this.status = status; 
    } 
} 
+0

你說你要發送JSON,但你的Retorift接口描述您要發送的形式 – Divers

+0

您的Web服務調用後,什麼類型的數據要退從服務器?這些數據與'用戶'模型有關嗎? –

+0

我怎樣才能發送與我的界面Json?服務器不返回任何內容。 –

回答

2

您將需要一個單獨的模型類來處理Web服務的響應:

public class Response { public String response; public int status; }

,並把這個類來代替User在改造Web服務調用。 1.您smsPincodeCheckApiInterface接口的方法:

在更換UserResponse。 2. Call<Response> call = apiService.smsPincodeCheck(getPhoneNuber, email); 3.

call.enqueue(new Callback<Response>() { 
      @Override 
      public void onResponse(Call<Response> call, Response<Response> response) { 
       if(response.isSuccess()){ 
        Toast.makeText(RegisterThreeActivity.this, "200", Toast.LENGTH_SHORT).show(); 
       } 

       if(response.code() == 401){ 
        Toast.makeText(RegisterThreeActivity.this, "401", Toast.LENGTH_SHORT).show(); 
       } 
      } 

      @Override 
      public void onFailure(Call<Response> call, Throwable t) { 

      } 
     }); 
+0

您的意思是call.enqueue(new Callback ()而不是call.enqueue(new Callback ()?? –

+0

是的,試着改變它,你還需要在ApiInterface接口的'smsPincodeCheck'方法中用'Response'來改變'User'。 –

+0

這不起作用。隨着您的更改,我有一個不兼容的類型:致電 smsPincodeCheck(@Body用戶用戶); 致電 call = apiService.smsPincodeCheck(user); –