如何使用volley解析Json String中的数据?

时间:2016-05-04 18:42:12

标签: android json listview

enter image description here

在第一项活动中,我们将仅在列表视图中显示已购买日期和总数量。当用户点击列表项时,我们将在下一个活动中显示项目和数量详细信息。

[点击日期之后用户将转到下一个活动,它将显示产品名称n数量]

这是我试过的代码

    private ArrayList<parse> milkParss;


    //for Adapter
    private DateAdapter adapter ;
     //Initializing  the listview
        listView_1 = (ListView) findViewById(R.id.listView_1);
        //to get the data from method getData
        adapter = new DateAdapter(this,R.layout.simple_list,);

        listView_1.setAdapter(adapter);

        listView_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent2 = new Intent(MonthlySummaryActivity.this, UpdateDailySupply.class);
                intent2.putExtra(KEY_DATE, );
                startActivity(intent2);
            }
        });


        //initializing the milkpar
        milkPar = new ArrayList<parse>();

               private void sendRequest() {

        //for processing dialog box

        final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);

        //  JSONArrayRequest req = new JsonObjectRequest(Request.Method.GET, urlJsonArray, null, new Response.Listener<JSONObject>() {
        JsonArrayRequest req = new JsonArrayRequest(Request.Method.GET, urlJsonArray, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());
                try {
                    //parsing the value of jsaon array
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject person = (JSONObject) response.get(i);
                        String da = person.getString("date");
                        String pr = person.getString("product");
                        String na = person.getString("Person");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(MonthlySummaryActivity.this, "ERROR" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }

                },
                   new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MonthlySummaryActivity.this, "error::" + error.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
}





public class DateAdapter extends BaseAdapter {

    private List<parse> list = new ArrayList<parse>();
    private Context context ;

    public DateAdapter(List<parse> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       View view = convertView ;
        if (view == null ){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.simple_list,null);

        }
        return convertView ;
    }
}

[2]

1 个答案:

答案 0 :(得分:0)

习惯Android的凌空一开始可能会让人感到困惑,但是一旦掌握了它,就会很容易。获得解析JSON的数据后,可以使用Intent中的bundle或extra将其传递给另一个活动。

RequestQueue queue = Volley.newRequestQueue(getActivity());
    StringRequest sr = new StringRequest(Request.Method.POST, myUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    //getting the response
                    try {
                        JSONArray jsonArray = new JSONArray(s);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            String name;
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            name= jsonObject.getString("name");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.wtf("error", e.getMessage());
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    }) {
//sending a request
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("id", id);
            return params;
        }
    };
    queue.add(sr);