我想在Android中使用列表视图显示用户的家庭数据。这是我的GSON:
{
"id": 0022,
"date_otor": null,
"agama": {
"agama": "CHRISTIAN"
"id_Agama": "1"
},
"hubungans": {
"id": "10",
"hubungan": "FATHER"
},
"jeniskelamin": {
"jenisKelamin": "1",
"kelamin": "Pria"
},
"golDarah": "",
"statusNikah": "0",
"nama": "JACKSON WANG",
"tgl_Lahir": "1960-12-18 00:00:00",
"noPegawai": "111111",
"tempatLahir": "SINGAPORE"
},
{
"id": 0023,
"date_otor": null,
"agama": {
"agama": "CHRISTIAN"
"id_Agama": "1"
},
"hubungans": {
"id": "21",
"hubungan": "MOTHER"
},
"jeniskelamin": {
"jenisKelamin": "2",
"kelamin": "Wanita"
},
"golDarah": "",
"statusNikah": "0",
"nama": "TITAN LOUI",
"tgl_Lahir": "1991-05-19 00:00:00",
"noPegawai": "111111",
"tempatLahir": "JAKARTA"
},
{
"id": 0025,
"date_otor": null,
"agama": {
"agama": "CHRISTIAN"
"id_Agama": "1"
},
"hubungans": {
"id": "22",
"hubungan": 2nd child"
},
"jeniskelamin": {
"jenisKelamin": "2",
"kelamin": "Wanita"
},
"golDarah": "",
"statusNikah": "0",
"nama": "CHRISTY",
"tgl_Lahir": "1994-05-16 00:00:00",
"noPegawai": "111111",
"tempatLahir": "SINGAPORE"
},
{
"id": 0024,
"date_otor": null,
"agama": {
"agama": "CHRISTIAN"
"id_Agama": "1"
},
"hubungans": {
"id": "3",
"hubungan": 1ST CHILD"
},
"jeniskelamin": {
"jenisKelamin": "1",
"kelamin": "Pria"
},
"golDarah": "",
"statusNikah": null,
"nama": "MICHAEL",
"tgl_Lahir": "2007-10-16 00:00:00",
"noPegawai": "111111",
"tempatLahir": SINGAPORE
}
]
这是我的FamilyAdapters.java:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.example.cobaakses.R;
import java.util.List;
import androidx.recyclerview.widget.RecyclerView;
public class FamilyAdapters extends RecyclerView.Adapter<FamilyAdapters.FamilyViewHolder>{
private List<familylistresponse> families;
private int rowLayout;
private Context context;
public static class FamilyViewHolder extends RecyclerView.ViewHolder{
LinearLayout familyLayout;
TextView fname;
TextView fstatus;
TextView fgender;
TextView freligion;
public FamilyViewHolder(View view){
super(view);
familyLayout = view.findViewById(R.id.family_layout);
fname = view.findViewById(R.id.tv_fname);
fstatus = view.findViewById(R.id.tv_fhubungan);
fgender = view.findViewById(R.id.tv_fjeniskelamin);
freligion = view.findViewById(R.id.tv_fagama);
}
}
public FamilyAdapters( Context context,List<familylistresponse> families, int rowLayout) {
this.families = families;
this.rowLayout = rowLayout;
this.context = context;
}
@Override
public FamilyAdapters.FamilyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new FamilyViewHolder(view);
}
@Override
public void onBindViewHolder(FamilyViewHolder holder, final int position){
holder.fname.setText(families.get(position).getNama());
holder.fgender.setText(families.get(position).getJeniskelamin());
holder.fstatus.setText(families.get(position).getHubungans());
holder.freligion.setText(families.get(position).getAgama());
}
@Override
public int getItemCount(){
return families.size();
}
}
这是我的家人。java:
package com.example.cobaakses.cobaarrayretrofit;
import com.example.cobaakses.Agama;
import com.example.cobaakses.Hubungans;
import com.example.cobaakses.Jeniskelamin;
import com.google.gson.annotations.SerializedName;
public class familylistresponse {
@SerializedName("noPegawai")
private String noPegawai;
@SerializedName("date_otor")
private Object dateOtor;
@SerializedName("jeniskelamin")
private Jeniskelamin jeniskelamin;
@SerializedName("keterangan")
private String keterangan;
@SerializedName("hubungans")
private Hubungans hubungans;
@SerializedName("tgl_Lahir")
private String tglLahir;
@SerializedName("nama")
private String nama;
@SerializedName("agama")
private Agama agama;
@SerializedName("statusNikah")
private String statusNikah;
@SerializedName("tempatLahir")
private String tempatLahir;
@SerializedName("id")
private int id;
@SerializedName("golDarah")
private String golDarah;
public void setNoPegawai(String noPegawai){
this.noPegawai = noPegawai;
}
public String getNoPegawai(){
return noPegawai;
}
public void setDateOtor(Object dateOtor){
this.dateOtor = dateOtor;
}
public Object getDateOtor(){
return dateOtor;
}
public void setJeniskelamin(Jeniskelamin jeniskelamin){
this.jeniskelamin = jeniskelamin;
}
public Jeniskelamin getJeniskelamin(){
return jeniskelamin;
}
public void setKeterangan(String keterangan){
this.keterangan = keterangan;
}
public String getKeterangan(){
return keterangan;
}
public void setHubungans(Hubungans hubungans){
this.hubungans = hubungans;
}
public Hubungans getHubungans(){
return hubungans;
}
public void setTglLahir(String tglLahir){
this.tglLahir = tglLahir;
}
public String getTglLahir(){
return tglLahir;
}
public void setNama(String nama){
this.nama = nama;
}
public String getNama(){
return nama;
}
public void setAgama(Agama agama){
this.agama = agama;
}
public Agama getAgama(){
return agama;
}
public void setStatusNikah(String statusNikah){
this.statusNikah = statusNikah;
}
public String getStatusNikah(){
return statusNikah;
}
public void setTempatLahir(String tempatLahir){
this.tempatLahir = tempatLahir;
}
public String getTempatLahir(){
return tempatLahir;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setGolDarah(String golDarah){
this.golDarah = golDarah;
}
public String getGolDarah(){
return golDarah;
}
@Override
public String toString(){
return
"ListUserResponse2{" +
"noPegawai = '" + noPegawai + '\'' +
",date_otor = '" + dateOtor + '\'' +
",jeniskelamin = '" + jeniskelamin + '\'' +
",keterangan = '" + keterangan + '\'' +
",hubungans = '" + hubungans + '\'' +
",tgl_Lahir = '" + tglLahir + '\'' +
",nama = '" + nama + '\'' +
",agama = '" + agama + '\'' +
",statusNikah = '" + statusNikah + '\'' +
",tempatLahir = '" + tempatLahir + '\'' +
",id = '" + id + '\'' +
",golDarah = '" + golDarah + '\'' +
"}";
}
}
这个是ApiClient.java:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
public static final String BASE_URL = "IPAddress/employee/family/";
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;
}
}
这个是我的ListKeluarga.java活动,将显示家庭列表:
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.example.cobaakses.MainInterface;
import com.example.cobaakses.R;
import java.util.List;
public class ListKeluarga extends AppCompatActivity {
private static final String TAG = ListKeluarga.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_keluarga);
SharedPreferences preferences = getSharedPreferences("MyPref",0);
String tokenShared = preferences.getString("userToken",null);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.families_rv);
recyclerView.setLayoutManager(new LinearLayoutManager(ListKeluarga.this));
MainInterface apiService =
ApiClient.getClient().create(MainInterface.class);
Call<familyuserresponse> call = apiService.getFamily("010713", "Bearer "+tokenShared);
call.enqueue(new Callback<familyuserresponse>() {
@Override
public void onResponse(Call<familyuserresponse>call, Response<familyuserresponse> response) {
int statusCode = response.code();
List<familylistresponse> familiess = response.body().getResults();
Log.d(TAG, "Number of families received: " + familiess.size());
}
@Override
public void onFailure(Call<familyuserresponse>call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
}
}
这是我的MainInterface.java:
@GET("http://IPAddress/employee/family/111111")
Call<familyuserresponse> getFamily(@Query("nik") String nik, @Header("Authorization") String auth);
我已经尝试为Family.java.中的每个变量使用字符串,但它仍然显示了这一点:
e/recycle view:未连接适配器;正在跳过句柄为0 x8c 2 f 8150 E/ListKeluarga:com . Google . gson . jsonsyntaxexception:Java . lang . illegalstateexception:应为BEGIN_OBJECT,但在第1行第2列path $处为BEGIN_ARRAY
你分享的JSON实际上是无效的。首先,您希望它是一个对象数组,所以像这样用< code>[]包装它。
[
{
"agama": {
"agama": "CHRISTIAN",
"id_Agama": "1"
},
"date_otor": null,
"golDarah": "",
"hubungans": {
"hubungan": "FATHER",
"id": "10"
},
"id": 22,
"jeniskelamin": {
"jenisKelamin": "1",
"kelamin": "Pria"
},
"nama": "JACKSON WANG",
"noPegawai": "111111",
"statusNikah": "0",
"tempatLahir": "SINGAPORE",
"tgl_Lahir": "1960-12-18 00:00:00"
},
{
"agama": {
"agama": "CHRISTIAN",
"id_Agama": "1"
},
"date_otor": null,
"golDarah": "",
"hubungans": {
"hubungan": "MOTHER",
"id": "21"
},
"id": 23,
"jeniskelamin": {
"jenisKelamin": "2",
"kelamin": "Wanita"
},
"nama": "TITAN LOUI",
"noPegawai": "111111",
"statusNikah": "0",
"tempatLahir": "JAKARTA",
"tgl_Lahir": "1991-05-19 00:00:00"
}
]
现在,您需要做的就是将返回类型更改为List。
@GET("http://IPAddress/employee/family/111111")
Call<ArrayList<familyuserresponse>> getFamily(@Query("nik") String nik, @Header("Authorization") String auth);
因为,你的错误说预期BEGIN_OBJECT但BEGIN_ARRAY你告诉Gson你有一个你类型的对象。你没有。您有一个类型对象的数组。
Call<List<familyuserresponse>> call= apiService.getFamily("010713", "Bearer "+tokenShared);
call.enqueue(new Callback<List<familyuserresponse>>() {
@Override
public void onResponse(Call<List<familyuserresponse>> call, Response<List<familyuserresponse>> response) {
List<familylistresponse> dataArrayList = response.body();
}
@Override
public void onFailure(Call<List<familyuserresponse>> call, Throwable t) {
Log.e("Error",t.getMessage());
}
});
问题内容: 我有要解析的JSON文件。JSON文件(“ myfile”)的格式如下: 我想从语言级别检索键2的值(ŚrednioZaawansowany)。 接下来做什么?我如何迭代呢? 问题答案: 也许您没有使用Java库的最新版本的JSON。 已有很长时间没有更新,而2个月前已更新。 可以在GitHub上找到,这是其仓库的链接:https : //github.com/douglascrock
我正在使用Thymeleaf和hibernate创建一个简单的SpringBootWeb应用程序。所以计划从产品实体中显示产品类别,其中每个类别属于多个产品,一个产品只有一个类别。 希望显示所有产品,但有些产品无法使用嵌套属性显示产品类别。 它给出了以下错误 此应用程序没有/error的显式映射,因此您将其视为回退。 Sun 1月31日03:46:01 CET 2021有一个意外错误(type =
问题内容: 您有任何想法如何从对象中检索全部吗?我需要将 for循环 更改为 流 “同类”代码。 非常感谢任何帮助 问题答案: 您可以与涉及的串联和三元运算符混合使用,例如:
我试图从API调用中检索数据并将其传递给另一个服务。我收到的数据是在一个特定的JSON结构,我想把它映射到一个结构,但没有多层次的数据。我尝试了点符号来访问更深的值,但它不起作用。 基本上,我试图得到一个包含一系列“问题”(key、self、description)的结构,但没有“fields.description”结构。 JSON: 结构: 预期/期望的结构: 有可能吗?如果是,怎么做?使用嵌
我想寻求帮助,了解如何从Firestore中检索称为“城市”的嵌套地图数组的数据 到MutableList ,然后我想将其插入到回收器视图中,其中来自“区域”的数据用于标题,数据“城市”用于常规列表项。 区域数据: 可变列表 , 当我按照 Alex Mamo https://medium.com/firebase-tips-tricks/how-to-map-an-array-of-objects
我创造了一个这样的形式 如果它没有验证,我将重新显示填充了数据的表单。填充问题数据并不难 但是,我无法从答案模型(具有内容和链接字段)的会话哈希中获取嵌套值。这不起作用。 我觉得是因为hash对象中的0,1,2迭代。例如,这是hash对象的一个片段(完整显示在下面) 但我不知道在构建问题答案时如何表达这一点 当我尝试构建时,在尝试从哈希对象中提取数据时,我不会表示迭代。你能解释一下我如何修改这一行