问题描述
我是Json解析的新手.我正在尝试使用JsonArrayRequest
读取json数据,但在发送参数和使用POST方法时有点困惑.在JsonObjectRequest
的情况下,我可以发送方法类型,URL,参数,但是在JsonArrayRequest
中如何发送参数例如用户名和密码,并使用POST方法.请帮帮我.这是我编写的代码.
I am newbie to Json parsing. I am trying to read a json data using JsonArrayRequest
but I am little confused in sending parameters and use POST method.In case of JsonObjectRequest
i can send the method type,url,params but In JsonArrayRequest
how to send params like username and password and use POST method.please help me out.here is the code i have written.
final JsonArrayRequest jsonObjReq = new JsonArrayRequest(MySingleton.getInstance().getDOWNLOAD_SERVICES_URL(), new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
if(response==null) {
pDialog.hide();
}
for (int i = 0; i < response.length(); i++) {
jresponse = response.getJSONObject(i);
}
//String service_response = response.getString("SvcTypeDsc");
Toast.makeText(getActivity().getApplicationContext(), "services" + jresponse, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
Log.d("soservices", "sos" + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Login request", "Error: " + error.getMessage());
Log.d("Volley Error:", "Volley Error:" + error.getMessage());
Toast.makeText(getActivity(), "Unable to connect to server, try again later", Toast.LENGTH_LONG).show();
pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("uniquesessiontokenid","39676161-b890-4d10-8c96-7aa3d9724119");
params.put("loginname", userDet.getSAID());
params.put("password", "23295");
return super.getParams();
}
@Override
public int getMethod() {
try {
getParams();
} catch (AuthFailureError authFailureError) {
authFailureError.printStackTrace();
}
return super.getMethod();
}
};
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
我的json数据如下:
my json data is like:
{
"username":"rushi",
"firstname": "abc",
},
{
"username": "xyz",
"firstname": "vxa",
},
未发送参数.请帮我解决这个问题.预先谢谢你.
The params are not being sent. Please help me solving this.Thank you in advance.
推荐答案
当使用JsonArrayRequest和POST方法时,这解决了我传递参数的问题.
This solved my problem to pass params when JsonArrayRequest and POST method is used.
Volley.newRequestQueue(getActivity())
.add(new JsonRequest<JSONArray>(Request.Method.POST,
MySingleton.getInstance().getDOWNLOAD_SERVICES_URL(),
jsonobj.toString(),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
Log.d("response", "res-rec is" + jsonArray);
if (jsonArray == null) {
pDialog.dismiss();
Snackbar.make(myview, "No services found", Snackbar.LENGTH_LONG).show();
} else {
for (int i = 0; i < jsonArray.length(); i++) {
try {
pDialog.dismiss();
JSONObject jsonObject = jsonArray.getJSONObject(i);
String desc = jsonObject.getString("SvcTypeDsc");
String image_url = jsonObject.getString("ThumbnailUrl");
// al_ImageUrls.add(image_url);
al_list_of_services.add(desc);
ad_servicesadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, al_list_of_services);
lv_webservicesList.setAdapter(ad_servicesadapter);
Log.d("imageurls", "imagesurl " + image_url);
Log.d("services-list", "list is " + desc + " " + i);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d("Login request", "Error: " + volleyError.getMessage());
Log.d("Volley Error:", "Volley Error:" + volleyError.getMessage());
Toast.makeText(getActivity(), "Unable to connect to server, try again later", Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
// params.put("uniquesessiontokenid", "39676161-b890-4d10-8c96-7aa3d9724119");
params.put("uniquesessiontokenid", userDet.getSessionToken());
params.put("said", userDet.getSAID());
params.put("SOId", "23295");
return super.getParams();
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse networkResponse) {
try {
String jsonString = new String(networkResponse.data,
HttpHeaderParser
.parseCharset(networkResponse.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser
.parseCacheHeaders(networkResponse));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
// return null;
}
}
);
这篇关于在Volley中使用JsonArrayRequest时如何发布请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!