2017-07-09 5 views
0

Ich habe ein Problem mit JSON-Objekt in Android-Anwendung von PHP-Skript. Auf Log.i (finalJson, "json object") sehe ich logcat, dass die json async-Task nur das erste Element aus der Liste zurückgibt. Ich teste meine php in POSTMAN und es sieht so aus:AsyncTask gibt nur zuerst JSONObject in JSONArray zurück

[ 
     { 
      "ID": "1", 
      "Description": "Plisani meda", 
      "Price": "1000", 
      "Image": "http://www.volimsvojdom.rs/wf-proizvodiPics/47188/plisana-igracka-mis-u-pantalonama-LB80322.jpg" 
     }, 
     { 
      "ID": "2", 
      "Description": "poni", 
      "Price": "2000", 
      "Image": "http://www.volimsvojdom.rs/wf-proizvodiPics/47188/plisana-igracka-mis-u-pantalonama-LB80322.jpg" 
     } 
    ] 

public class JSONTask extends AsyncTask<String,String,List<Prozivod>> 
{ 

    @Override 
    protected List<Prozivod> doInBackground(String... params) { 
     HttpURLConnection connection=null; 
     BufferedReader reader=null; 

     try { 
      URL url=new URL(params[0]); 
      connection=(HttpURLConnection)url.openConnection(); 
      connection.connect(); 
      InputStream stream=connection.getInputStream(); 
      reader=new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer=new StringBuffer(); 
      String line=""; 
      while((line=reader.readLine())!=null) 
      { 
      buffer.append(line); 

      } 

      String finalJson=buffer.toString(); 
      JSONArray jsonArray=new JSONArray(finalJson); 
      Log.i(finalJson,"json object"); 
      List<Products> listOfItems=new ArrayList<>(); 
      for(int i=0;i<jsonArray.length();i++) 
      { 
       JSONObject itemJson=jsonArray.getJSONObject(i); 
       Products p=new Products(); 
       p.setProizvodID(proizvodJson.getInt("ID")); 
       p.setCena(proizvodJson.getInt("Price")); 
       p.setOpis(proizvodJson.getString("Description")); 
       p.setSlika(proizvodJson.getString("Image")); 
       listOfItems.add(p); 
       return listOfItems; 

      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     catch (JSONException e) { 
      e.printStackTrace(); 
     }finally { 
      if(connection!=null){connection.disconnect();} 
     } 
     try{if(reader!=null){reader.close();}} 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null ; 
    } 

Dankten für jede Hilfe!

+3

Sie kehren aus der For-Schleife zurück. Deshalb wurde es nach einer Iteration beendet. Wahrscheinlich möchten Sie die Liste nach der for-Schleife zurückgeben – Apoorv

+0

return listOfItems; verschiebe diese Zeile außerhalb von for loop –

+1

Vorschlag: Verwenden Sie Retrofit anstelle von AsyncTask –

Antwort

1

Sie geben die Liste unmittelbar nach der Iteration zurück. Versuchen Sie dies:

for(int i=0;i<jsonArray.length();i++) 
     { 
      JSONObject itemJson=jsonArray.getJSONObject(i); 
      Products p=new Products(); 
      p.setProizvodID(proizvodJson.getInt("ID")); 
      p.setCena(proizvodJson.getInt("Price")); 
      p.setOpis(proizvodJson.getString("Description")); 
      p.setSlika(proizvodJson.getString("Image")); 
      listOfItems.add(p); 
     } 
     return listOfItems; 

Hoffe, dass dies hilft.

+0

Vielen Dank! –

Verwandte Themen