2016-08-22 5 views
0

Ich entwickle eine App, hier möchte ich abgerufene Elemente der Tabelle habe ich versucht folgenden Code, aber es gibt Fehler bei diesem Linie JSONObject c = result.getJSONObject(0);. Wie löse ich das?Wie zu lösen org.json.JSONException: Index 0 außerhalb des Bereichs [0..0) mit Android

//java file 
    public class Details extends Activity { 

      TextView uid; 
      TextView name1, amount1; 
      TextView email1; 
      Button Btngetdata; 

      //URL to get JSON Array 
      private static String url = "http://example.in/ticket1.php"; 

      //JSON Node Names 
      private static final String TAG_USER = "result"; 
      // private static final String TAG_ID = "id"; 
      private static final String TAG_NAME = "pname"; 
      private static final String TAG_AMOUNT = "pamount"; 

      JSONArray result = null; 

      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 

       setContentView(R.layout.details); 

       Btngetdata = (Button)findViewById(R.id.getdata); 
       Btngetdata.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View view) { 
         new JSONParse().execute(); 

        } 
       }); 

      } 

      private class JSONParse extends AsyncTask<String, String, JSONObject> { 
       private ProgressDialog pDialog; 
       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        uid = (TextView)findViewById(R.id.uid); 
        name1 = (TextView)findViewById(R.id.name); 
        amount1 = (TextView)findViewById(R.id.email); 
        pDialog = new ProgressDialog(Details.this); 
        pDialog.setMessage("Getting Data ..."); 
        pDialog.setIndeterminate(false); 
        pDialog.setCancelable(true); 
        pDialog.show(); 

       } 

       @Override 
       protected JSONObject doInBackground(String... args) { 
        JSONParser jParser = new JSONParser(); 

        // Getting JSON from URL 
        JSONObject json = jParser.getJSONFromUrl(url); 
        return json; 
       } 
       @Override 
       protected void onPostExecute(JSONObject json) { 
        pDialog.dismiss(); 
        try { 
         // Getting JSON Array 
         result = json.getJSONArray(TAG_USER); 
         JSONObject c = result.getJSONObject(0); 

         // Storing JSON item in a Variable 
        // String id = c.getString(TAG_ID); 
         String name = c.getString(TAG_NAME); 
         String amount = c.getString(TAG_AMOUNT); 

         //Set JSON Data in TextView 
        // uid.setText(id); 
         name1.setText(name); 
         amount1.setText(amount); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
      } 

     } 
+0

was Ihre Antwort? –

+0

posten Sie Ihre Fehlerprotokolle auch –

+0

Bitte stellen Sie Ihre Anfrage und Antwort erstens –

Antwort

0

überprüfen Sie die Größe des Ergebnis Array, bevor Sie über sie iterieren.

@Override 
protected void onPostExecute(JSONObject json) { 
{ 
pDialog.dismiss(); 
try { 
    // Getting JSON Array 
    result = json.getJSONArray(TAG_USER); 
    if (result.length() > 0) { 
    JSONObject c = result.getJSONObject(0); 

    // Storing JSON item in a Variable 
    // String id = c.getString(TAG_ID); 
    String name = c.getString(TAG_NAME); 
    String amount = c.getString(TAG_AMOUNT); 

    //Set JSON Data in TextView 
    // uid.setText(id); 
    name1.setText(name); 
    amount1.setText(amount); 
    } 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 
} 
Verwandte Themen