0

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;
    }
}
3
  • Do you want to separate all values by tags (e.g. "id", "nombre") in an individual list? Commented Jul 18, 2017 at 3:11
  • Yes, it's what i want Commented Jul 18, 2017 at 3:13
  • You will have to Parse it and save it in a model object. create a model class with id, nombre etc as parameters. Then create a class object and assign each parameter its value using jsonArray.getJSONObject(i).getString("id") and then add that object to arraylist to have a list of objects having individual values. While populating you can use the same list to show on UI Commented Jul 18, 2017 at 3:25

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

4 Comments

Where do you give values to 'idList','nombreList', etc. after the 'for'?
Oh thank you so much! now i understand the way to do it, thank you again!
could you tell me how to access for example to the element 1 when use MyResponse.GetidList?
Answer updated! Also check the getDataJSON() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.