2016-09-08 14 views
-2

Ich versuche, JSON von einer Website analysieren und in einer ListView anzeigen. Allerdings, wenn ich mein Programm laufen lasse, erhalte ich einen JSON-Parsing-Fehler (siehe unten):JSON Parsing Converted Fehler

Wie kann ich dieses Problem beheben?

Mein Code ist unten:

public class MainActivity extends AppCompatActivity { 

    private String TAG = MainActivity.class.getSimpleName(); 

    private ProgressDialog pDialog; 
    private ListView lv; 

    // URL to get contacts JSON 
    private static String url = "https://www.netdata.com/JSON/9c9cc5eb"; 

    ArrayList<HashMap<String, String>> hissetList; 

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

     hissetList = new ArrayList<>(); 

     lv = (ListView) findViewById(R.id.list); 

     new GetHisse().execute(); 
    } 

    /** 
    * Async task class to get json by making HTTP call 
    */ 
    private class GetHisse 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 hisseler = jsonObj.getJSONArray("array"); 

        // looping through All Contacts 
        for (int i = 3; i <6; i++) { 
         JSONObject c = hisseler.getJSONObject(i); 

         String id = c.getString("ID"); 
         String name = c.getString("dc_HisseKodu"); 
         // String email = c.getString("dc_WebAdresi"); 
        // String address = c.getString("dc_Adresi"); 
         // String gender = c.getString("dc_BagimsizDenetimKurulusu"); 
         //String mobile = c.getString("dc_Telefon"); 
         // Phone node is JSON Object 


         // tmp hash map for single contact 
         HashMap<String, String> hisse = new HashMap<>(); 

         // adding each child node to HashMap key => value 
         hisse.put("id", id); 
         hisse.put("name", name); 
         // hisse.put("email", email); 
        // hisse.put("mobile", mobile); 

         // adding contact to contact list 
         hissetList.add(hisse); 
        } 
       } 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, hissetList, 
        R.layout.list_item, new String[]{"name", "id"}, new int[]{R.id.name, 
        R.id.mobile}); 

      lv.setAdapter(adapter); 
     } 

    } 
} 
+0

Beitrag vollständige Fehlermeldung –

+1

Bitte senden Sie das vollständige Ausnahme Text anstelle eines Screenshot. – Alexander

+0

Link unten auf der Seite Json Parsing Fehler: Wert –

Antwort

0

Die Antwort in Form von JSONArray nicht JSONObject empfangen ist. statt:

JSONObject jsonObj = new JSONObject(jsonStr); 

Verwendung dieses

JSONArray jsonArray = new JSONArray(jsonStr); 
0

Es gibt keine ein JSON-Array "Array" genannt. Sie ändern Code wie diese

JSONArray jsonObj = new JSONArray(jsonStr); 
for(int i = 0; i<jsonObj.length(); i++){ 
    JSONObject c = jsonObj .getJSONObject(i); 
    ...... 
}