2017-01-25 3 views
0

JSON LInk - http://jsonviewer.stack.hu/#http://www.saveme.ie/api/savings/Android - tableau JSON sans Parsing Nom Array

Im essayant de chercher un JSON pour remplir un ListView dans Android avec cette tâche de fond, mais le dire qu'il ya un problème avec le JSON. Il semble que ce soit parce que le tableau n'a pas de nom.

Comment est-ce que je modifierais le code afin qu'il puisse aller chercher le tableau sans nom?

private class GetSavings extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     HttpHandler sh = new HttpHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       **JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       JSONArray savings = jsonObj.getJSONArray("savings"); 
       // looping through All Contacts 
       for (int i = 0; i < savings.length(); i++) { 
        JSONObject c = savings.getJSONObject(i); 
        String title = c.getString("title"); 

       }** 
      } catch (final JSONException e) { 
       Log.e(TAG, "Json parsing error: " + e.getMessage()); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Json parsing error: " + e.getMessage(), 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 

      } 
     } else { 
      Log.e(TAG, "Couldn't get json from server."); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Couldn't get json from server. Check LogCat for possible errors!", 
          Toast.LENGTH_LONG) 
          .show(); 
       } 
      }); 

     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       MainActivity.this, savingsList, 
       R.layout.list_item, new String[]{"name", "email", 
       "mobile"}, new int[]{R.id.name, 
       R.id.email, R.id.mobile}); 

     lv.setAdapter(adapter); 
    } 

} 
+0

Pourriez-vous partager l'erreur spécifique que vous obtenez? – DCruz22

+0

Premier lien est le JSON - http://jsonviewer.stack.hu/#http://www.saveme.ie/api/savings/ – ChrisM

+0

Ceci est ma réponse - http://stackoverflow.com/a/41616005/ 4741746 –

Répondre

1

lieu de jeter dans JSONObject, la jeter dans JSONArray.

JSONArray jsonObj = new JSONArray(jsonStr); 

puis de parcourir votre tableau.

+0

Pouvez-vous fournir un petit échantillon de code pour expliquer le tableau de cheminement? merci – ChrisM

+0

pour (int i = 0; i