无法显示json对象的输出

时间:2019-04-16 19:20:32

标签: java android json retrofit

我想从json对象解析json并将其放在textview上。我尝试了一些方法,但是失败了。错误:

  

期望BEGIN_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT。

API服务:完整版http://139.255.86.189:83/service/api/checklistpertanyaan/1

{
  "success": true,
  "data": [
    {
      "idRchecklistpompa": "1",
      "nmChecklist": "Membersihkan Body Pompa"
    },
    {
      "idRchecklistpompa": "2",
      "nmChecklist": "Membersihkan Kabel Tray Pompa"
    },

Harian.java

public class Harian {
    @SerializedName("idRchecklistpompa")
    @Expose
    private String idRchecklistpompa;

    @SerializedName("nmChecklist")
    @Expose
    private String nmChecklist;

    public String getIdRchecklistpompa() {
        return idRchecklistpompa;
    }

    public String getNmChecklist() {
        return nmChecklist;
    }

    public void setIdRchecklistpompa(String idRchecklistpompa) {
        this.idRchecklistpompa = idRchecklistpompa;
    }

    public void setNmChecklist(String nmChecklist) {
        this.nmChecklist = nmChecklist;
    }
}

MainActivity.java

public class HarianActivity extends AppCompatActivity {

    private TextView textViewResult;
    /*private static String url = "http://139.255.86.189:83/service/api/checklistpertanyaan/1";*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_harian);

        textViewResult = findViewById(R.id.text_view_result);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://139.255.86.189:83/service/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        HarianApi harianApi = retrofit.create(HarianApi.class);
        Call<List<Harian>> call = harianApi.getHarian();
        call.enqueue(new Callback<List<Harian>>() {
            @Override
            public void onResponse(Call<List<Harian>> call, Response<List<Harian>> response) {
                if (!response.isSuccessful()) {
                    textViewResult.setText("CodeL " + response.code());
                    return;
                }

                List<Harian> harians = response.body();

                for (Harian harian : harians) {
                    String content = "";
                    content += "ID " + harian.getIdRchecklistpompa() + "\n";
                    content += "NAMA " + harian.getNmChecklist() + "\n";

                    textViewResult.append(content);
                }
            }

            @Override
            public void onFailure(Call<List<Harian>> call, Throwable t) {
                textViewResult.setText(t.getMessage());
            }
        });
    }
}

3 个答案:

答案 0 :(得分:0)

我认为您的课程有问题。响应与您的pojo类不同。请参见json to pojo,然后根据生成的pojo创建模型。

答案 1 :(得分:0)

我希望封装List中的Harian的JSON看起来像这样:

[
    {
      "idRchecklistpompa": "1",
      "nmChecklist": "Membersihkan Body Pompa"
    },
    {
      "idRchecklistpompa": "2",
      "nmChecklist": "Membersihkan Kabel Tray Pompa"
    }
]

相反,您的开头是:

{
  "success": true,
  "data": [
  ...

因此,API返回List<Harian>是不正确的。相反,您的API应该返回另一个看起来更像的类:

public class Container {

    @SerializedName("success")
    private boolean success;

    @SerializedName("data")
    List<Harian> data;

    public static class Harian {
        @SerializedName("idRchecklistpompa")
        @Expose
        private String idRchecklistpompa;

        @SerializedName("nmChecklist")
        @Expose
        private String nmChecklist;

        public String getIdRchecklistpompa() {
            return idRchecklistpompa;
        }

        public String getNmChecklist() {
            return nmChecklist;
        }

        public void setIdRchecklistpompa(String idRchecklistpompa) {
            this.idRchecklistpompa = idRchecklistpompa;
        }

        public void setNmChecklist(String nmChecklist) {
            this.nmChecklist = nmChecklist;
        }
    }
}

并让您的Retrofit API返回Container而不是List<Harian>

答案 2 :(得分:0)

不确定我是否理解,但是要调试问题,我将要做的是:

1.-作为字符串检查响应是否为格式正确的JSON字符串。

try {
   JSONObject jsonObject = new JSONObject(response.code());
} catch (JSONException e) {
   e.printStackTrace();
}

1.5.-检查该字符串是JSONObject还是JSONArray

2.-可能尝试从该字符串创建JSONObject / JSONArray,以查看它是否触发异常。

try {
    String nmChecklist = jsonObject.getString("nmChecklist");
} catch (JSONException e) {
    e.printStackTrace();
}

3.-尝试解析JSONObject,但检查异常:

String nmChecklist = jsonObject.has("nmChecklist") && !jsonObject.isNull("nmChecklist") ? jsonObject.getString("nmChecklist") : null;

4.-如果由于某些对象可能具有或不具有键或值,而要避免出现异常,则:

{{1}}

我希望这会有所帮助。