【问题标题】:null response from retrofit改造的无效响应
【发布时间】:2018-10-01 06:48:41
【问题描述】:

在我的项目中,我有两个模型。我想将客户的信息发送到服务器并从服务器获得响应。 在 android studio 中,我想通过改造将模型发送到服务器。当我发送请求时,改造返回一个空响应给我。谁能帮帮我?
这是我的 customers_role 模型:

public class customers_role {

@SerializedName("customers_models")
private customers_model customers_models;
@SerializedName("mobile")
private String mobile;
@SerializedName("phone")
private String phone;
@SerializedName("first_job")
private String first_job;
@SerializedName("second_job")
private String second_job;
@SerializedName("first_address")
private String first_address;
@SerializedName("second_address")
private String second_address;
@SerializedName("postal_code")
private String postal_code;
@SerializedName("nik_name")
private String nik_name;
@SerializedName("description")
private String description;
@SerializedName("created_at")
private String created_at;
@SerializedName("updated_at")
private String updated_at;
@SerializedName("status")
private boolean status;

public customers_role() {
}
}

这里是customers_model:

public class customers_model {
@SerializedName("id")
private Integer id;
@SerializedName("name")
private String name;
@SerializedName("family")
private String family;
@SerializedName("code")
private String code;
@SerializedName("father")
private String father;
@SerializedName("birthday")
private String birthday;
@SerializedName("created_at")
private String created_at;
@SerializedName("updated_at")
private String updated_at;
@SerializedName("status")
private boolean status;

public customers_model() {
}

public customers_model(String name, String family, String code, String father, String birthday) {
    this.name = name;
    this.family = family;
    this.code = code;
    this.father = father;
    this.birthday = birthday;
}
}

我的界面是:

@Headers("Accept: application/json")
    @POST("customers")
    Call<customers_role> send_info(@Header("Authorization") String token, @Body customers_role info);

谢谢

`

【问题讨论】:

  • 服务器的响应是什么?首先在邮递员中检查
  • 你能分享一下json响应吗?
  • @JeelVankhede 我用邮递员对其进行了测试,效果很好,但在 android studio 中我的回复为空
  • @BayarŞahintekin 这是邮递员 [ "فیلد customer.name الزامی است", "فیلد customer.family الزامی است", "فیلد customer.code الزامی است", "فیلد customer.father 的回复", "فیلد customer.birthday الزامی است" ]
  • @zahras 看起来你在邮递员中得到字符串 json 数组,你期待你的模型在改造中做出回应,这就是为什么它是 null

标签: android retrofit


【解决方案1】:

当你得到按摩فیلد فلان اجرابیه
我认为你需要填写项目:
namefamilycodefatherbirthday
然后像使用customers_role 一样为它们编写接口

也许应该是这样的:

@Headers("Accept: application/json")
@POST("customers")
Call<customers_role> send_info(@Header("Authorization") String token, @Body customers_model model);

希望对你有帮助

【讨论】:

  • 我将customers_role更改为customers_model,但它还返回null!
  • 你的回复又是这样的? => [ "فیلد customer.name الزامی است", "فیلد customer.family الزامی است", "فیلد customer.code الزامی است", "فیلد customer.father الزامی است", "فیلد customer.birthday跨度>
  • 您是否通过调试来测试您的项目?确保您发送正确的数据
【解决方案2】:

当我遇到@Post 注释问题时,我会这样做:

@Headers("Accept: application/json")
@POST
Call<customers_role> send_info(@Url String url, @Body customers_role customersRole);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2019-03-14
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2015-11-03
    相关资源
    最近更新 更多