简体   繁体   English

如何使用Volley解析JSON

[英]how to parse JSON using Volley

i am using volley library for post data over api...in here the Content-Type is "application/json" .... 我正在使用凌空库通过api发布数据...在这里Content-Type是“ application / json”...。

how can i implement this type of json data : 我如何实现这种类型的json数据:

URL: Base_URL + dorequisition
{
  "submitted_by_employee_id": 1,
  "name": "Technolive SF for TC",
  "bags_thana_wise":    [
              {
                "tiger": "10000",
                "extreme": "5000",
                "opc": "3000",
                "three_rings": "4000",
                "buffalo_head": "2000", 
               },
            ],

  "free_bag": "500",
  "landing_price": "450",
  "transport_cost_ex_factory_rate": "450",
  "amount_taka": "four hundred fifty",
  "bank_name": "IFIC Bank Limited",
  "delivery_point": "Banani",
  "upto_today": "450",
  "bag_type": "swing",
  "remark": "Good Cement",
  "token": "2cbf1cefb6fe93673565ed2a0b2fd1a1"
}

api implementation sample : api实现示例:

  public void APIRetailVisitInfo() {

        for (int i = 0; i < retailVisitInfoList.size(); i++) {
            System.out.println("token id:::" + token + "   :::"+employee_id);

            Map<String, String> jsonParams = new HashMap<String, String>();

            jsonParams.put("submitted_by_employee_id", employee_id);
            jsonParams.put("retailer_name", retailVisitInfoList.get(i).getRetails_name());
            jsonParams.put("retailer_phone", retailVisitInfoList.get(i).getRetailer_contact_no());
            jsonParams.put("retailer_address", retailVisitInfoList.get(i).getRetails_address());
            jsonParams.put("remarks", retailVisitInfoList.get(i).getRemarks());
            jsonParams.put("token", token);


            JsonObjectRequest myRequest = new JsonObjectRequest(
                    Request.Method.POST,
                    "http://technolive.co/retailvisitinfo",
                    new JSONObject(jsonParams),

                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {


                            try {
                                code = response.getString("code");
                                //token = response.getString("token");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                            if (code.equals("200")) {

                                System.out.println("APICompetetorBasedOnMArketPrice:::" + code + "   :::");
                            }


                            //  System.out.println("token:::" + token+"   :::");*/
                            //   verificationSuccess(response);

                            System.out.println("APICompetetorBasedOnMArketPrice:::" + response + "   :::");

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            //  verificationFailed(error);
                        }
                    }) {

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();

                    String auth = token;
                    headers.put("Content-Type", "application/json; charset=utf-8");
                    headers.put("User-agent", "My useragent");
//                headers.put("Authorization", auth);
                    return headers;
                }


            };

            RequestQueue requestQueue = Volley.newRequestQueue(RetailVisitInfoActivity.this);
            requestQueue.add(myRequest);
            //  MyApplication.getInstance().addToRequestQueue(myRequest, "tag");

        }
    }

can anyone help me please........ 谁能帮我........

It is rather simple. 这很简单。 First let's start with bags_thana_wise. 首先让我们从bags_thana_wise开始。 bags_thana_wise is a JSONArray. bags_thana_wise是一个JSONArray。 It contains one object. 它包含一个对象。 So lets create the object. 因此,让我们创建对象。

JSONObject json1 = new JSONObject();
json1.put("tiger","10000";
json1.put("extreme","5000";

and so on..Now put this json object into an array 依此类推..现在将这个json对象放入数组

JSONArray jsonArray = new JSONArray();
jsonArray.put(json1);

Now just add this array and other values to another json object 现在,只需将此数组和其他值添加到另一个json对象

JSONObject finalObject = new JSONObject();
finalObject.put("submitted_by_employee_id","1");
finalObject.put("name","Technolive SF for TC");
//put the jsonArray 
finalObject.put("bags_thana_wise",jsonArray);
finalObject.put("free_bag","500");
.
.
.
finalObject.put("token","2cbf1cefb6fe93673565ed2a0b2fd1a1");

Now make the following volley request 现在发出以下齐射要求

JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.POST,
            myURL, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
          //Handle response
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(PopupActivity.this,"Can not reach server",Toast.LENGTH_LONG).show();
        }

    }){
        @Override
        public String getBodyContentType(){
            return "application/json; charset=utf-8";
        }

        @Override
        public byte[] getBody() {
            try {
                return finalObject == null ? null: finalObject.getBytes("utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(RetailVisitInfoActivity.this);
        requestQueue.add(jsonRequest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM