为了账号安全,请及时绑定邮箱和手机立即绑定

ListView 未使用 Retrofit 显示所有 JSON 数据

ListView 未使用 Retrofit 显示所有 JSON 数据

慕桂英3389331 2022-11-02 17:02:25
我正在尝试将所有 JSON 结果放入我的列表视图中。我已成功检索数据,但它只显示一个数据(例如,如果 JNE,只显示 OKE 结果)。我正在使用改造。下面是我想在我的列表视图中显示的文档中的示例 JSON 数据。请帮助我为什么它没有显示所有结果。谢谢你。{ "rajaongkir":{  "query":{     "origin":"501",     "destination":"114",     "weight":1700,     "courier":"jne"  },  "status":{     "code":200,     "description":"OK"  },  "origin_details":{     "city_id":"501",     "province_id":"5",     "province":"DI Yogyakarta",     "type":"Kota",     "city_name":"Yogyakarta",     "postal_code":"55000"  },  "destination_details":{     "city_id":"114",     "province_id":"1",     "province":"Bali",     "type":"Kota",     "city_name":"Denpasar",     "postal_code":"80000"  },  "results":[     {        "code":"jne",        "name":"Jalur Nugraha Ekakurir (JNE)",        "costs":[           {              "service":"OKE",              "description":"Ongkos Kirim Ekonomis",              "cost":[                 {                    "value":38000,                    "etd":"4-5",                    "note":""                 }              ]           },           {              "service":"REG",              "description":"Layanan Reguler",              "cost":[                 {                    "value":44000,                    "etd":"2-3",                    "note":""                 }              ]           },           {              "service":"SPS",              "description":"Super Speed",              "cost":[                 {                    "value":349000,                    "etd":"",                    "note":""                 }              ]           },           {              "service":"YES",              "description":"Yakin Esok Sampai",              "cost":[                 {                    "value":98000,                    "etd":"1-1",                    "note":""                 }              ]           }        ]     }    ]    }    }
查看完整描述

1 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

public void getCoast(String origin,

                     String destination,

                     String weight,

                     String courier) {


    Retrofit retrofit = new Retrofit.Builder()

            .baseUrl(ApiUrl.URL_ROOT_HTTPS)

            .addConverterFactory(GsonConverterFactory.create())

            .build();


    ApiService service = retrofit.create(ApiService.class);

    Call<ItemCost> call = service.getCost(

            "c5333cdcc37b3511c909088d99587fd8",

            origin,

            destination,

            weight,

            courier

    );



    call.enqueue(new Callback<ItemCost>() {

        @Override

        public void onResponse(Call<ItemCost> call, Response<ItemCost> response) {


            Log.v("wow", "json : " + new Gson().toJson(response));

            progressDialog.dismiss();


            if (response.isSuccessful()) {


                int statusCode = response.body().getRajaongkir().getStatus().getCode();


                if (statusCode == 200) {

                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                    View alertLayout = inflater.inflate(R.layout.custom_results, null);

                    alert = new AlertDialog.Builder(MainActivity.this);

                    alert.setTitle("Result Cost");

                    alert.setMessage("this result your search");

                    alert.setView(alertLayout);

                    alert.setCancelable(true);


                    ad = alert.show();



                    String originCity = response.body().getRajaongkir().getOriginDetails().getCityName();

                    String originPostalCode = response.body().getRajaongkir().getOriginDetails().getPostalCode();

                    String destinationCity = response.body().getRajaongkir().getDestinationDetails().getCityName();

                    String destinationPostalCode = response.body().getRajaongkir().getDestinationDetails().getPostalCode();


                    //results

                    List<com.bagicode.cekongkir.model.cost.Result> ListResults = response.body().getRajaongkir().getResults();


                    //costs

                    List<com.bagicode.cekongkir.model.cost.Cost> ListCosts = response.body().getRajaongkir().getResults().get(0).getCosts();


                   //cost

                   List<com.bagicode.cekongkir.model.cost.Cost_> ListCost = response.body().getRajaongkir().getResults().get(0).getCosts().get(0).getCost();




                    mListView = (ListView) alertLayout.findViewById(R.id.listItem);

                    adapter_results = new ResultsAdapter(MainActivity.this, originCity, originPostalCode, destinationCity, destinationPostalCode, ListResults, ListCosts, ListCost);



                    mListView.setAdapter(adapter_results);

                    mListView.setClickable(true);


                    adapter_results.notifyDataSetChanged();




                } else {


                    String message = response.body().getRajaongkir().getStatus().getDescription();

                    Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();

                }


            } else {

                String error = "Error Retrive Data from Server !!!";

                Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();

            }


        }


        @Override

        public void onFailure(Call<ItemCost> call, Throwable t) {


            progressDialog.dismiss();

            Toast.makeText(MainActivity.this, "Message : Error " + t.getMessage(), Toast.LENGTH_SHORT).show();

        }

    });


}


查看完整回答
反对 回复 2022-11-02
  • 1 回答
  • 0 关注
  • 71 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信