【问题标题】:Retrofit Returns Null value改造返回 Null 值
【发布时间】:2017-02-10 17:51:31
【问题描述】:

我是 Android 网络新手。我无法弄清楚为什么 phonone

的值为 null

这是 JSON 数据的示例:

{
    "columns":{ <some data here> },
    "rows":[  
    {  
        "timestamp":"28/08/2016 14:11:46",
        "name":"Mohammed Sohail",
        "phoneno.":8142629002,
        "event-name":"Roadies",
        "branch":"IT",
        "year":3
    },
    {  
        "timestamp":"28/08/2016 14:13:03",
        "name":"Shaik Asaduddin",
        "phoneno.":8143026049,
        "event-name":"Ted talk",
        "branch":"IT",
        "year":3
    }
}

我能够获取行数组中的每个值,除了“phoneno”它给我空值

这是我的课

注册类:

public class Registration  {
    private Columns columns;
    private List<Row> rows = null;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public Columns getColumns() {
        return columns;
    }

    public void setColumns(Columns columns) {
        this.columns = columns;
    }

    public List<Row> getRows() {
        return rows;
    }

    public void setRows(List<Row> rows) {
        this.rows = rows;
    }

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }


}

行类:

public class Row {

    private String timestamp;
    private String name;
    private String phoneno;
    private String eventName;
    private String branch;
    private String year;

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneno() {
        return phoneno;
    }

    public void setPhoneno(String phoneno) {
        this.phoneno = phoneno;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getBranch() {
        return branch;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

}

这是我用来设置文本视图的适配器

public class RegistrationAdapter extends RecyclerView.Adapter<RegistrationAdapter.RegistrationViewHolder> {

    private List<Row> rows;
    private Context context;
    private int rowLayout;

    public RegistrationAdapter(List<Row> rows) {
        this.rows=rows;
    }


    @Override
    public RegistrationAdapter.RegistrationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.registration_item_view, parent, false);
        return new RegistrationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RegistrationViewHolder holder, int position) {

        holder.studentName.setText(rows.get(position).getName());
        holder.studentPhone.setText(String.valueOf(rows.get(position).getPhoneno()));
        holder.studentBranch.setText(rows.get(position).getBranch());
        holder.studentYear.setText(rows.get(position).getYear());
    }

    @Override
    public int getItemCount() {
        return rows.size();
    }

    public class RegistrationViewHolder extends RecyclerView.ViewHolder {

        LinearLayout studentLayout;
        TextView studentName;
        TextView studentPhone;
        TextView studentBranch;
        TextView studentYear;


        public RegistrationViewHolder(View itemView) {
            super(itemView);
            studentLayout=(LinearLayout)itemView.findViewById(R.id.studentLayout);
            studentName=(TextView)itemView.findViewById(R.id.studentName);
            studentPhone=(TextView)itemView.findViewById(R.id.studentPhoneNumber);
            studentBranch=(TextView)itemView.findViewById(R.id.studentBranch);
            studentYear=(TextView)itemView.findViewById(R.id.studentYear);

        }
    }

    public RegistrationAdapter( List<Row> rows, int rowLayout,Context context) {
        this.rows = rows;
        this.rowLayout = rowLayout;
        this.context = context;
    }
}

【问题讨论】:

  • 你需要@Expose PoJo中的字段
  • 但是我可以在不使用@Expose 的情况下获得其余的值,这是强制性的吗?
  • 请检查您的 JSON 数据,我可以在“phoneno.”中看到一个点 (.),是不是拼写错误?如果不是,那么您可以检查是否是导致问题的原因。
  • @DavidAguilar 是的,点是问题

标签: android networking retrofit


【解决方案1】:

也许是一个错字,但在你的 json 中你有“phoneno”。并且您的行类指定 phoneno。

【讨论】:

  • 我搜索了这个问题,发现如果 json 和 class 变量具有不同的名称,但在我的情况下它们具有相同的名称,就会发生这种情况
  • 我可以在你的 json 中看到不是“phoneno”而是“phoneno”。您在 json 索引的末尾有一个点。但是您的类变量中缺少那个点。
  • 感谢您的帮助,问题已解决,我错过了点
【解决方案2】:

你可以使用 jsonschema2pojo 从 json 对象生成类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 2021-10-28
    • 2018-01-22
    • 2022-01-05
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多