0

I wanna create a list item by retrieving data from JSON, But it failed, the log chat are:

  • Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

  • Error parsing data org.json.JSONException: No value for title`

this is my JSON:

{
"berita": [
    {
        "id_berita": "1",
        "judul_berita": "abcdefgh",
        "gambar_berita": "media.jpg",
        "isi_berita": "arsgrdgrdgrdgrfdrffffffffffrvfsffsfsfsf"
    },
    {
        "id_berita": "2",
        "judul_berita": "ijklmnopqrst",
        "gambar_berita": "back.jpg",
        "isi_berita": "kbkjcbkjbcjkbckjanckjnakjcbjkabcjkabcjabcjhbcjdbcjdb"
    }
]

and this my code

protected Void doInBackground(Void... voids) {
        HttpHandler sh = new HttpHandler();
        String jsonStr = sh.makeServiceCall(url);
        Log.e(TAG, "Response from url : "+jsonStr);
        if(jsonStr != null){
            try{
                JSONObject jsonObject = new JSONObject(jsonStr);

                //getting node json array
                JSONArray berita = jsonObject.getJSONArray("berita");

                //looping semua berita
                for(int i = 0; i < berita.length(); i++){
                    JSONObject c = berita.getJSONObject(i);

                    String id_berita = c.getString("id_berita");
                    String judul_berita = c.getString("judul_berita");

                    HashMap<String, String> Berita = new HashMap<>();
                    Berita.put("id_berita", id_berita);
                    Berita.put("judul_berita", judul_berita);

                }
            }
            catch (final JSONException e){
                Log.e(TAG, "JSON parsing error : "+e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(BeritaActivity.this, "JSON parsing error"+e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }

        }else {
            Log.e(TAG, "Tidak bisa mendapat JSON dari server");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(BeritaActivity.this, "Tidak bisa mendapat JSON dari server", Toast.LENGTH_SHORT).show();
                }
            });
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if(pDialog.isShowing()){
            pDialog.dismiss();
        }
        ListAdapter adapter = new SimpleAdapter(
                BeritaActivity.this, beritaList,
                R.layout.list_item, new String[]{"judul_berita"},
                new int[]{R.id.judulBerita});
        lv.setAdapter(adapter);
    }
}
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Gus
  • 1
  • could you please specify which line isnt working? – WhatsThePoint May 25 '17 at 12:35
  • im get error fom the line, catch (final JSONException e) – Gus May 25 '17 at 12:48
  • Error parsing data org.json.JSONException: No value for title` => In your Json doesn't have "id_berita" or "judul_berita". for safe your app: you should check before getString. Use .has(String) and .isNull(String) Link: https://stackoverflow.com/questions/12585492/how-to-test-if-a-jsonobject-is-null-or-doesnt-exist – MrTy May 26 '17 at 04:42
  • Thx all, this problem have ben solved.. – Gus May 27 '17 at 16:12

0 Answers0