java - 放入 JSON 对象中的变量

标签 java android json loopj

您好,我正在通过 .php 文件中的 JSON 回显将测试 Android 应用程序链接到 MySQL 数据库。

我能够用整个数据填充 ArrayList,但现在我想将数据分离到变量中,但我无法真正找到方法。

我正在使用loopJ库进行JSON通信

我的MainActivity.java 公共(public)类 MainActivity 扩展 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;
    }
}

最佳答案

首先,为您所需的字段创建一个具有不同 getter/setter 的 pojo 类。

  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;
    }

}

现在解析每个响应并添加到 MyResponse 类型列表中

         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;
                    }

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();

关于java - 放入 JSON 对象中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45156769/

相关文章:

java - 使 GridLayout 中的按钮填充空白区域

Java 使用 Collection.contains() 测试对象的特定属性

java - 将绘制的 Canvas 组件存储到数组中

java - 安卓 : how to send array as parameter in wsdl request

android - NoClassDefFoundError 在带有 Gradle 的 Android 上使用 Jackson 2.2.x

python - For 循环遍历图像文件夹并输出到单个 JSON 文件

java - jackson 解码问题

Android IAB 错误 - 需要身份验证

java - MySQL 连接 - java.sql.SQLException : Unable to connect to any hosts due to exception

json - 如何在全日历中将事件显示为彩色点?