0

I'm using JSONParser.java to show JSON data from a url

the url and the data are correct, they show some values of JSON (more than one)

but why this show only one value?

ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(ArtikelURL);

        try {
            artikels = json.getJSONArray(TAG_ARTIKEL);

            for(int i = 0; i < artikels.length(); i++){
                JSONObject c = artikels.getJSONObject(i);

                // Storing each json item in variable
                String tanggal = c.getString(TAG_TGL);
                String judul = c.getString(TAG_JUDUL);
                String kutipan = c.getString(TAG_KUTIPAN);

                HashMap<String, String> map = new HashMap<String, String>();            
                map.put(TAG_TGL, tanggal);
                map.put(TAG_JUDUL, judul);
                map.put(TAG_KUTIPAN, kutipan);
                artikelList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

This is my JSON data:

{
   "status":"1",
   "total":20,
   "artikel":[
      {
         "tanggal":"2013-08-07",
         "judul":"Article One",
         "kutipan":"Example of article quote..."
      },
      {
         "tanggal":"2013-07-23",
         "judul":"Article Two",
         "kutipan":"Example of article quote......"
      },
      {
         "tanggal":"2013-07-22",
         "judul":"Article Three",
         "kutipan":"Example of article quote......"
      },
      {
         "tanggal":"2013-03-16",
         "judul":"Article Four"",
         "kutipan":"Example of article quote,..."
      }
   ]
}
user2790880
  • 87
  • 1
  • 10

2 Answers2

1

Your JSON is invalid, there's an error in your line 22 at 3rd object of your array

"judul":"Article Four"",

Correct JSON is

{
"status": "1",
"total": 20,
"artikel": [
    {
        "tanggal": "2013-08-07",
        "judul": "Article One",
        "kutipan": "Example of article quote..."
    },
    {
        "tanggal": "2013-07-23",
        "judul": "Article Two",
        "kutipan": "Example of article quote......"
    },
    {
        "tanggal": "2013-07-22",
        "judul": "Article Three",
        "kutipan": "Example of article quote......"
    },
    {
        "tanggal": "2013-03-16",
        "judul": "Article Four",
        "kutipan": "Exampleofarticlequote,..."
    }
]
}

Ok, Now you are getting Network related exception so, solution to this is, you've to use demo code like

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new NetworkThread().execute();
}

class NetworkThread extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... voids) {

        ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(ArtikelURL);

        try {
            JSONArray artikels = json.getJSONArray("artikel");

            for(int i = 0; i < artikels.length(); i++){
                JSONObject c = artikels.getJSONObject(i);

                // Storing each json item in variable
                String tanggal = c.getString(TAG_TGL);
                String judul = c.getString(TAG_JUDUL);
                String kutipan = c.getString(TAG_KUTIPAN);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_TGL, tanggal);
                map.put(TAG_JUDUL, judul);
                map.put(TAG_KUTIPAN, kutipan);
                artikelList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

    }
}

Don't forget to add this to AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET"/>
Jaydipsinh Zala
  • 16,558
  • 7
  • 41
  • 45
0

Your JSON Is Invalid But assuming you have an outer Json Array for the JsonObject This implementation would work for your nested Json

      ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJSONFromUrl(ArtikelURL);

    try {
       JSONArray  a = json.getJSONArray(TAG_JSON);

        for(int i = 0; i < a.length(); i++){
        HashMap<String, String> map = new HashMap<String, String>();   
         JSONObject c = a.getJSONObject(i);

            map.put(TAG_TOTAL, c.getString(TAG_TOTAL));
            map.put(TAG_STATUS, c.getString(TAG_STATUS));
            artikelList.add(map);
            JSONArray  akel = e.getJSONArray(artikel);
            if(akel != null){
          for(int j=0;j<akel.length();j++){
          HashMap<String, String> map2 = new HashMap<String, String>();
          JSONObject f = akel.getJSONObject(j);
          map2.put(TAG_TGL, f.getString(TAG_TGL));
          map2.put(TAG_JUDUL, f.getString(TAG_JUDUL));
          map2.put(TAG_KUTIPAN, f.getString(TAG_KUTIPAN));
          }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
James Wahome
  • 588
  • 11
  • 31