Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

java - Retrieve data to ListView from API in android

I need to retrieve data to listview from this link http://www.autotrack.rs/android_juzna_backa/get_voznja.php?. I need to send key, with value-POST method, to get something like this: http://www.autotrack.rs/android_juzna_backa/get_voznja.php?voznja_id=42. My code is bellow: Thanks. ERROR Value null at data of type org.json.JSONObject$1 cannot be converted to JSONArray

 public class fetchData extends AsyncTask<String, String, String> {

        @Override
        public void onPreExecute() {
            super.onPreExecute();
            swipeRefresh.setRefreshing(true);
        }

        @Override
        protected String doInBackground(String... params) {
            arrayList.clear();
            String result = null;
            try {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("text/plain");
                RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                        .addFormDataPart("voznja_id", "42")
                        .build();
                Request request = new Request.Builder()
                        .url("http://www.autotrack.rs/android_juzna_backa/get_voznja.php")
                        .method("POST", body)
                        .build();
                try {
                    response = client.newCall(request).execute();
//                System.out.println(response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return "";


            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }

        @Override
        public void onPostExecute(String rezultat) {

            try {
                rezultat = response.body().string();
            }catch (Exception m)
            {
                m.printStackTrace();
            }
                Intent intent;
                Gson gson = new Gson();
                Type type = new TypeToken<Voznja>() {
                }.getType();

                Voznja voznja = gson.fromJson(rezultat, type);

                Intent im = new Intent(getApplicationContext(), Unos.class);
                im.putExtra("voznja", voznja);
               // startActivity(im);
             swipeRefresh.setRefreshing(false);
                try {
                    JSONObject object = new JSONObject(rezultat);
                   JSONArray array = object.getJSONArray("data");

                    for (int i = 0; i < array.length(); i++) {

                        JSONObject jsonObject = array.getJSONObject(i);
                        String id = jsonObject.getString("id");
                        String vozilo_id = jsonObject.getString("vozilo_id");
                        String vozac_id = jsonObject.getString("vozac_id");
                        String pocetna_kilometraza = jsonObject.getString("pocetna_kilometraza");
                        String pocetno_vreme = jsonObject.getString("pocetno_vreme");
                        String razlog = jsonObject.getString("razlog");
                        String zavrsna_kilometraza = jsonObject.getString("zavrsna_kilometraza");
                        String zavrsno_vreme = jsonObject.getString("zavrsno_vreme");
                        String moto_sati = jsonObject.getString("moto_sati");
                        String id_projekat_jb = jsonObject.getString("id_projekat_jb");

                        ItemModel model = new ItemModel();
                        model.setVoznja_id(id);
                        model.setVoziloId(vozilo_id);
                        model.setVozac_id(vozac_id);
                        model.setPocetnaKilometraza(pocetna_kilometraza);
                        model.setPocetnoVreme(pocetno_vreme);
                        model.setRazlog(razlog);
                        model.setZavrsnaKilometraza(zavrsna_kilometraza);
                        model.setZavrsnoVreme(zavrsno_vreme);
                        model.setMotoSati(moto_sati);
                        model.setProjekatId(id_projekat_jb);
                        arrayList.add(model);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                VoznjaAdapter adapter = new VoznjaAdapter(ListaVoznji.this, arrayList);
                listView.setAdapter(adapter);
question from:https://stackoverflow.com/questions/65904926/retrieve-data-to-listview-from-api-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your api response is JSONObject.

{"data":{"id":"42","vozilo_id":"777777003561","vozac_id":"2","pocetna_kilometraza":"50000","pocetno_vreme":"2020-12-25 07:15:00","projekat":null,"razlog":"Sastanak","zavrsna_kilometraza":"50150","zavrsno_vreme":"2020-12-25 10:45:00","moto_sati":"3","insert_time":"2021-01-08 10:47:33.683885","id_projekat_jb":"330"}}

So you should use JSONObject instead JSONArray

            try {
                JSONObject object = new JSONObject(rezultat);
               JSONObject jsonObject = object.getJSONObject("data");

                    String id = jsonObject.getString("id");
                    String vozilo_id = jsonObject.getString("vozilo_id");
                    String vozac_id = jsonObject.getString("vozac_id");
                    String pocetna_kilometraza = jsonObject.getString("pocetna_kilometraza");
                    String pocetno_vreme = jsonObject.getString("pocetno_vreme");
                    String razlog = jsonObject.getString("razlog");
                    String zavrsna_kilometraza = jsonObject.getString("zavrsna_kilometraza");
                    String zavrsno_vreme = jsonObject.getString("zavrsno_vreme");
                    String moto_sati = jsonObject.getString("moto_sati");
                    String id_projekat_jb = jsonObject.getString("id_projekat_jb");

                    ItemModel model = new ItemModel();
                    model.setVoznja_id(id);
                    model.setVoziloId(vozilo_id);
                    model.setVozac_id(vozac_id);
                    model.setPocetnaKilometraza(pocetna_kilometraza);
                    model.setPocetnoVreme(pocetno_vreme);
                    model.setRazlog(razlog);
                    model.setZavrsnaKilometraza(zavrsna_kilometraza);
                    model.setZavrsnoVreme(zavrsno_vreme);
                    model.setMotoSati(moto_sati);
                    model.setProjekatId(id_projekat_jb);
                    arrayList.add(model);
            } catch (JSONException e) {
                e.printStackTrace();
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...