我想通過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。我剛剛開始進行改造,所以我不能正確理解它。請告訴我如何正確檢索預期值。
我建議你隱藏自己的API密鑰和用戶名和密碼:) –