Gson.fromJson结果为空

时间:2019-03-05 16:00:05

标签: java php android mysql

我正在尝试构建Office应用程序,并试图将json加载到文本字段,但结果始终为空。我的代码有什么问题?

这是我的InstansiLain.java

public class InstansiLain implements Parcelable {

  @SerializedName("id_instansi_lain")
  private Integer idInstansiLain;
  @SerializedName("nama_pegawai")
  private String namaPegawai;
  @SerializedName("nama_jabatan")
  private String namaJabatan;
  @SerializedName("nama_perusahaan")
  private String namaPerusahaan;

  public static InstansiLain objectFromData(String str) {
    return new Gson().fromJson(str, InstansiLain.class);
  }

  public static InstansiLain objectFromData(String str, String key) {
    try {
        JSONObject jsonObject = new JSONObject(str);

        return new Gson().fromJson(jsonObject.getString(str), InstansiLain.class);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
  }

  public static List<InstansiLain> arrayInstansiLainFromData(String str) {
    Type listType = new TypeToken<ArrayList<InstansiLain>>() {
    }.getType();
    return new Gson().fromJson(str, listType);
  }

  public static List<InstansiLain> arrayInstansiLainFromData(String str, String key) {

    try {
        JSONObject jsonObject = new JSONObject(str);
        Type listType = new TypeToken<ArrayList<InstansiLain>>() {
        }.getType();

        return new Gson().fromJson(jsonObject.getString(str), listType);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return new ArrayList();
  }

  public Integer getIdInstansiLain() {
    return idInstansiLain;
  }

  public void setIdInstansiLain(Integer idInstansiLain) {
    this.idInstansiLain = idInstansiLain;
  }

  public String getNamaPegawai() {
    return namaPegawai;
  }

  public void setNamaPegawai(String namaPegawai) {
    this.namaPegawai = namaPegawai;
  }

  public String getNamaJabatan() {
    return namaJabatan;
  }

  public void setNamaJabatan(String namaJabatan) {
    this.namaJabatan = namaJabatan;
  }

  public String getNamaPerusahaan() {
    return namaPerusahaan;
  }

  public void setNamaPerusahaan(String namaPerusahaan) {
    this.namaPerusahaan = namaPerusahaan;
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(this.idInstansiLain);
    dest.writeString(this.namaPegawai);
    dest.writeString(this.namaJabatan);
    dest.writeString(this.namaPerusahaan);
  }

  public InstansiLain() {
  }

  protected InstansiLain(Parcel in) {
    this.idInstansiLain = (Integer) in.readValue(Integer.class.getClassLoader());
    this.namaPegawai = in.readString();
    this.namaJabatan = in.readString();
    this.namaPerusahaan = in.readString();
  }

  public static final Parcelable.Creator<InstansiLain> CREATOR = new Parcelable.Creator<InstansiLain>() {
    @Override
    public InstansiLain createFromParcel(Parcel source) {
        return new InstansiLain(source);
    }

    @Override
    public InstansiLain[] newArray(int size) {
        return new InstansiLain[size];
    }
  };

这是我的适配器类,我不确定出现错误的部分

public class InstansiLainListAdapter extends RecyclerView.Adapter<InstansiLainListAdapter.ViewHolder> {

  private Context context;
  private List<InstansiLain> lf;

  public static class ViewHolder extends RecyclerView.ViewHolder {
    public LinearLayout layoutPegawaiParent;
    public TextView tvPegawai, tvJabatan, tvPangkat, tvKeterangan;
    public ImageView ivHapus;

    public ViewHolder(View view) {
        super(view);
        layoutPegawaiParent = (LinearLayout) view.findViewById(R.id.layoutPegawaiParent);
        tvPegawai = (TextView) view.findViewById(R.id.tvPegawai);
        tvJabatan = (TextView) view.findViewById(R.id.tvJabatan);
        tvPangkat = (TextView) view.findViewById(R.id.tvPangkat);
        tvKeterangan = (TextView) view.findViewById(R.id.tvKeterangan);

        ivHapus = (ImageView) view.findViewById(R.id.ivHapus);
    }
  }

  public InstansiLainListAdapter(Context context, List<InstansiLain> lf) {
    this.context = context;
    this.lf = lf;
  }

  @Override
  public InstansiLainListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_pegawai_sp, parent, false);

    ViewHolder vh = new ViewHolder(v);
    return vh;
  }

  @Override
  public void onBindViewHolder(final InstansiLainListAdapter.ViewHolder holder, final int position) {
    final InstansiLain f = lf.get(position);

    holder.tvPegawai.setText(f.getNamaPegawai().trim());
    holder.tvJabatan.setText(f.getNamaJabatan());
    holder.tvPangkat.setText(f.getNamaPerusahaan());

    holder.tvKeterangan.setVisibility(View.GONE);
    holder.ivHapus.setVisibility(View.VISIBLE);

    holder.ivHapus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lf.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(0, lf.size());
        }
    });
  }

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

  public List<InstansiLain> getData() {
    return lf;
  }
}

这是将json显示到textview的类的一部分

private void fillView(SuratMasuk sm) {
    refreshListPegawai(rvKepada, adapterKepada, lpKepada);
    kepadaExt = sm.getKepadaExt();
    isKepadaExt = (sm.getKepadaExt().length()>0 ? true : false);

    lsInstansiLain.clear();
    lsInstansiLain.addAll(InstansiLain.arrayInstansiLainFromData(sm.getInstansiLain()));
    refreshInstansiLain(rvInstansiLain, adapterInstansiLain, lsInstansiLain);
    Log.d("InsLain " + TagService.INSTANSI_LAIN, sm.getInstansiLain());

    etDari.setText(namaPegawaiDari.trim());

    etTanggal.setText(sm.getTanggalSurat());
    etNomor.setText(sm.getNomor());
    etSifat.setText(sm.getSifat());

    if (!sm.getLampiran().equals("-")) {
        String lampiran_arr[] = sm.getLampiran().split(" ");
        String lampiran = "";
        for (int i=0; i<lampiran_arr.length-1; i++) {
            lampiran += lampiran_arr[i] + " ";
        }
        etLampiran.setText(lampiran);

        etTipeLampiran.setVisibility(View.VISIBLE);
        etTipeLampiran.setText(lampiran_arr[lampiran_arr.length-1]);

        if (Integer.parseInt(lampiran_arr[0])>lk.size()) {
            lk.add(FileLampiran.fillLampiran());
        }
    }
    else {
        etLampiran.setText(sm.getLampiran());
        etTipeLampiran.setVisibility(View.GONE);
    }

    isi_surat = sm.getIsiSurat();
    etIsiSurat.setRichTextEditing(true, isi_surat);

    kalimat_penutup = sm.getTextPenutup();
    etKalimatPenutup.setRichTextEditing(true, kalimat_penutup);

    refreshFileLampiran(rvLampiran, adapterLampiran, lk);

    refreshListPegawaiSp(rvPegawaiPns, adapterPegawaiPns, lpPegawaiPns);

    etPaper.setText(sm.getPaper());

    refreshListPegawai(rvParaf, adapterParaf, lpParaf);
    refreshListPegawai(rvTembusan, adapterTembusan, lpTembusan);
}

0 个答案:

没有答案