如何使用Volley使用Parameter键和值解析数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Volley使用Parameter键和值解析数据相关的知识,希望对你有一定的参考价值。
带参数键和值的ARC Json数据视图
Java类
public void jsonParse() {
String fetchurl = getIntent().getStringExtra("fetch_url");
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, fetchurl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("org_list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject patient = jsonArray.getJSONObject(i);
String Id = patient.getString("orga_orgid");
textView1.append(Id + "." + "
");
String Name = patient.getString("orga_organame");
textView2.append(Name + " " + "
");
progressbar.setVisibility(View.GONE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded";
}
@Override
public byte[] getBody() {
Map<String, String> params = new HashMap<String, String>();
params.put("api_key","123456789" );
return super.getBody();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
requestQueue.add(request);
}
所以,这是我的java类,它不起作用;它显示此错误:
Unexpected response code 403 for http://xelwel.com.np/hamrosewaapp/api/get_organization_list
当我在检查ARC时从身体部位传递参数键和值,即“api_key”和“123456789”,然后只显示URL的数据。所以,我认为当我在java代码中使用hashmap for body时,它可能会在我的应用程序中显示数据。有人知道这个问题吗?
答案
试试这个
使用StringRequest
并在getParams()
方法中添加body的参数(你在postman中尝试)。
public void jsonParse() {
String fetchurl = "http://xelwel.com.np/hamrosewaapp/api/get_organization_list";
StringRequest stringRequest = new StringRequest(Request.Method.POST, fetchurl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.e("onResponse: ", response);
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//loading.dismiss();
Toast.makeText(TestActivity.this, "error", Toast.LENGTH_LONG).show();
Log.e("onErrorResponse: ", error + "");
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
//Adding the parameters to the request
params.put("api_key", "123456789");
Log.e("getParams: ", params + "");
return params;
}
};
//Adding request the the queue
requestQueue.add(stringRequest);
}
输出: - Web服务的响应。根据需要解析它。
{
"org_list": [
{
"orga_orgid": "1",
"orga_organame": "Civil Service Hospital",
"orga_orgaddress1": "New Banewshor",
"orga_orgaddress2": " Kathmandu,Nepal",
"orga_contactno": "977-1-4239161",
"orga_email": "info@xelwel.com.np",
"orga_website": "http://nationaltraumacenter.gov.np/",
"orga_headerimg": null,
"orga_footerimg": null,
"orga_software": "Biomedical Inventory System",
"orga_postdatead": null,
"orga_postdatebs": null,
"orga_posttime": null,
"orga_postmac": null,
"orga_postip": null,
"orga_postby": null,
"orga_modifydatead": null,
"orga_modifydatebs": null,
"orga_modifytime": null,
"orga_modifymac": null,
"orga_modifyip": null,
"orga_modifyby": null,
"orga_defaultpicker": "NP",
"orga_loginactivities": "A",
"orga_isdefaultdb": "Y",
"orga_isuseraccess": "N",
"orga_image": "trauma.png",
"orga_isactive": "Y"
},
{
"orga_orgid": "2",
"orga_organame": "National Trauma Center",
"orga_orgaddress1": "Mahankal",
"orga_orgaddress2": " Kathmandu,Nepal",
"orga_contactno": "977-1-4239161",
"orga_email": "info@xelwel.com.np",
"orga_website": "http://nationaltraumacenter.gov.np/",
"orga_headerimg": null,
"orga_footerimg": null,
"orga_software": "Assets Management System",
"orga_postdatead": null,
"orga_postdatebs": null,
"orga_posttime": null,
"orga_postmac": null,
"orga_postip": null,
"orga_postby": null,
"orga_modifydatead": null,
"orga_modifydatebs": null,
"orga_modifytime": null,
"orga_modifymac": null,
"orga_modifyip": null,
"orga_modifyby": null,
"orga_defaultpicker": "NP",
"orga_loginactivities": "A",
"orga_isdefaultdb": "Y",
"orga_isuseraccess": "N",
"orga_image": "trauma.png",
"orga_isactive": "Y"
}
]
}
以上是关于如何使用Volley使用Parameter键和值解析数据的主要内容,如果未能解决你的问题,请参考以下文章
如何在 AngularJS 中使用 ng-repeat 迭代键和值?
如何在iOS swift的tableview中使用json响应中的键和值?
如何使用 Swifty 动态创建具有多个键和值的 json 对象
如何使用python在mysql中插入python dict的键和值