Search code examples
javaandroidjsonloopj

Put in variables from JSON Object


Hi I'm linking a test android app to a MySQL database from an echo of JSON from a .php file.

I was able to populate an ArrayList with the whole data, but now i'd like to separate the data into variables and i can't really find out the way.

I'm using loopJ library to make the JSON communication

My MainActivity.java public class MainActivity extends AppCompatActivity {

    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.lvLista);

        getData();
    }

    public void getData()
    {
        AsyncHttpClient client = new AsyncHttpClient();
        String url = "http://www.kukitosoft.com/android/api.php";

       // RequestParams params = new RequestParams();
       // params.put("campo", "valor a fitrar");
        //Esto iria en el null del client.post

        client.post(url, null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200)
                {
                    loadList(getDataJSON(new String(responseBody)));
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


    public void loadList(ArrayList<String> data)
    {
        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
        list.setAdapter(adapter);
    }

    public ArrayList<String> getDataJSON(String response)
    {
        ArrayList<String> list = new ArrayList<String>();
        try
        {
            JSONArray jsonArray = new JSONArray(response);
            String texto;

            for(int i=0; i<jsonArray.length(); i++)
            {
                texto = jsonArray.getJSONObject(i).getString("id") + " " +
                        jsonArray.getJSONObject(i).getString("nombre") + " " +
                        jsonArray.getJSONObject(i).getString("descripcion") + " " +
                        jsonArray.getJSONObject(i).getString("calificacion") + " ";
                list.add(texto);
            }
        }

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

        return list;
    }
}

Solution

  • First, create a pojo class with different getter/setter for your required fields.

      public class MyResponse(){
    
        private List<String> idList;
        private List<String> nombreList;
        private List<String> descList;
        private List<String> califList;
    
        public void setIdList(List<String> list){
        this.idList = list;
        }
    
        public void getIdList(){
        return idList;
        }
    
        public void setNombreList(List<String> list){
        this.nombreList = list;
        }
    
        public void getNombreList(){
        return nombreList;
        }
    
        public void setDescList(List<String> list){
        this.descList = list;
        }
    
        public void getDescList(){
        return descList;
        }
    
        public void setCalifList(List<String> list){
        this.califList = list;
        }
    
        public void getCalifList(){
        return califList;
        }
    
    }
    

    Now parse each response and add in the list of MyResponse type

             public MyResponse getDataJSON(String response)
                        {
                            MyResponse myResponse = new MyResponse();
                            ArrayList<String> idList = new ArrayList<String>();
                            ArrayList<String> nombreList = new ArrayList<String>();
                            ArrayList<String> descList = new ArrayList<String>();
                            ArrayList<String> califList = new ArrayList<String>();
    
                            try
                            {
                                JSONArray jsonArray = new JSONArray(response);
    
                                for(int i=0; i<jsonArray.length(); i++)
                                {
                                  idList.add(jsonArray.getJSONObject(i).getString("id"));
    
       nombreList.add(jsonArray.getJSONObject(i).getString("nombre"));
                                  descList.add(jsonArray.getJSONObject(i).getString("descripcion"));
                                  califList.add(jsonArray.getJSONObject(i).getString("calificacion"));
                                }
    
                                myResponse.setIdList(idList);
                                myResponse.setNombreList(nombreList);
                                myResponse.setDescList(descList);
                                myResponse.setCalifList(califList);
    
                                return myResponse;
                            }
    
                            catch (Exception e)
                            {
                                e.printStackTrace();
                            }
    
                            return list;
                        }
    

    To retrieve back the data from getDataJSON()

         MyResponse myResponse = getDataJSON(new String(responseBody)));
         ArrayList<String> idList = myResponse.getIdList();
         ArrayList<String> nombreList = myResponse.getNombreList();
         ArrayList<String> descList = myResponse.getDescList();
         ArrayList<String> califList = myResponse.getCalifList();