AsyncTask只返回JSONArray中的第一个JSONObject

时间:2017-07-09 17:58:21

标签: android json android-asynctask

我在php脚本的android应用程序中遇到了json对象的问题。在Log.i上(finalJson," json对象")我看到i logcat,json异步任务只从列表中返回第一个元素。我在POSTMAN中测试我的php,它看起来像:

    [
        {
            "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 ;
   }

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您在迭代一次后返回列表。试试这个:

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;

希望这有帮助。