2017-06-15 53 views
0

我想通過Android中的Retrofit2通過JSON主體發佈請求。 我試圖在https://magicspree.com/restaurant/webservice/android/login上點擊api。
這裏是我的code.`無法從rest api檢索正確的值(RetroFit)

輸入JSON體(通過POST方法請求):

{ 
"restaurantapikey":"v4Vk2wEkzZfWGxeChavYKLnamLrXaDUJTpiInqeU", 
"restaurantusername":"dvar.rddwarka.del", 
"restaurantpassword":"password" 
} 

Restaurant.java(模型類):

public class Restaurant { 
    @SerializedName("restaurantApiKey") 
    private String restaurantApiKey; 
    @SerializedName("restaurantUserName") 
    private String restaurantUserName; 
    @SerializedName("restaurantPassword") 
    private String restaurantPassword; 
    @SerializedName("Status") 
    @Expose 
    private String status; 
    @SerializedName("Text") 
    @Expose 
    private String text; 
    @SerializedName("Restaurant_id") 
    @Expose 
    private Integer restaurantId; 
    public String getRestaurantApiKey() { 
    return restaurantApiKey; 
    } 
    public void setRestaurantApiKey(String restaurantApiKey) { 
    this.restaurantApiKey = restaurantApiKey; 
    } 
    public String getRestaurantUserName() { 
    return restaurantUserName; 
    } 
    public void setRestaurantUserName(String restaurantUserName) { 
    this.restaurantUserName = restaurantUserName; 
    } 
    public String getRestaurantPassword() { 
    return restaurantPassword; 
    } 
    public void setRestaurantPassword(String restaurantPassword) { 
    this.restaurantPassword = restaurantPassword; 
    } 
    public Restaurant(String apiKey, String userName,String password) { 
    this.restaurantApiKey = apiKey; 
    this.restaurantUserName = userName; 
    this.restaurantPassword=password; 
    } 
    public String getStatus() { 
    return status; 
    } 
    public void setStatus(String status) { 
    this.status = status; 
    } 
    public String getText() { 
    return text; 
    } 
    public void setText(String text) { 
    this.text = text; 
    } 
    public Integer getRestaurantId() { 
    return restaurantId; 
    } 
    public void setRestaurantId(Integer restaurantId) { 
    this.restaurantId = restaurantId; 
    } 
    public String toString(){ 
    return "id="+restaurantId+", status="+getStatus()+", text="+getText(); 
    } 
} 

ApiClient.java:

public class ApiClient { 
public static final String BASE_URL = 
"https://magicspree.com/restaurant/webservice/android/"; 
private static Retrofit retrofit = null; 
public static Retrofit getClient() { 
    if (retrofit==null) { 
     retrofit = new Retrofit.Builder() 
       .baseUrl(BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
    } 
    return retrofit; 
} 

}

ApiInterface.java:

public interface ApiInterface { 
@POST("login") 
Call<Restaurant> loginRestaurant(@Body Restaurant restaurant); 
} 

我的onCreate方法:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home_page); 
Restaurant r=new Restaurant("v4Vk2wEkzZfWGxeChavYKLnamLrXaDUJTpiInqeU","dvar.rddwarka.del","password"); 

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); 
    Call<Restaurant> call = apiService.loginRestaurant(r); 
    call.enqueue(new Callback<Restaurant>() { 
     @Override 
     public void onResponse(Call<Restaurant> call2, Response<Restaurant> response) { 
      System.out.println(response.body().toString()); 
     } 

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

     } 
}); 
} 

預計輸出JSON:

{"Status":"Success","Text":"Login Successful","Restaurant_id":27} 

的問題是,我得到的值狀態:失敗,文本:空和Restaurant_id:0。我剛剛開始進行改造,所以我不能正確理解它。請告訴我如何正確檢索預期值。

+0

我建議你隱藏自己的API密鑰和用戶名和密碼:) –

回答

0

這可能是幼稚,但我的鷹的眼睛:)我看到,在郵差你參數是所有小寫,而在你的代碼是駝峯(restaurantapikey VS restaurantApiKey) 這可能是你的後端與實現區分大小寫參數。 更改餐廳類使用:

@SerializedName("restaurantapikey") 
private String restaurantApiKey; 
@SerializedName("restaurantusername") 
private String restaurantUserName; 
@SerializedName("restaurantpassword") 
private String restaurantPassword; 
+0

感謝您指出了這一點。實際上,我在代碼中也使用了小寫參數,只是在Stack Overflow中輸入了錯誤:P反正我只是在URL中的第一個斜槓之後將我的端點更改爲所有內容,並使用「@field」而不是「@body」,現在它的工作。不管怎麼說,還是要謝謝你 :) – Pokeforbuff