gpt4 book ai didi

android - 如何使用 Volley 解析 JSON

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:26 25 4
gpt4 key购买 nike

我正在使用 volley 库通过 api 发布数据...这里的 Content-Type 是“application/json”....

我如何实现这种类型的 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 实现示例:

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

}
}

谁能帮帮我..........

最佳答案

其实很简单。首先让我们从 bags_thana_wise 开始。 bags_thana_wise 是一个 JSONArray。它包含一个对象。因此,让我们创建对象。

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

等等..现在把这个json对象放到一个数组中

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

现在只需将这个数组和其他值添加到另一个 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");

现在提出以下截击请求

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

关于android - 如何使用 Volley 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com