【问题标题】:Unable to retrieve correct values from rest api (RetroFit)无法从 rest api (RetroFit) 中检索正确的值
【发布时间】:2017-06-15 16:11:23
【问题描述】:

我正在尝试通过 Android 中的 Retrofit2 通过 JSON 正文发布请求。 我正在尝试通过https://magicspree.com/restaurant/webservice/android/login 访问 api。
这是我的代码。`

输入 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}

问题是我得到的值为 Status:Failed、Text:Null 和 Restaurant_id:0。我刚刚开始使用 Retrofit,所以我不能正确理解它。请告诉我如何正确检索预期值。

【问题讨论】:

  • 我建议您隐藏您的 API 密钥以及用户名和密码 :)

标签: java android retrofit


【解决方案1】:

这可能很天真,但用我的鹰眼:) 我看到在 Postman 中你的参数都是小写的,而在你的代码中它是 Camel 大小写(restaurantapikey 与餐厅ApiKey ) 可能是您的后端使用区分大小写的参数实现。 更改您的餐厅类以使用它:

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

【讨论】:

  • 感谢您指出这一点。实际上我在代码中也使用了小写参数,只是在堆栈溢出中输入错误:P 无论如何,我只是将端点更改为 url 中第一个斜杠之后的所有内容,并使用“@field”而不是“@body”,现在它的工作。无论如何谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
相关资源
最近更新 更多