我是 Json 解析的新手。我正在尝试使用读取 json 数据,JsonArrayRequest
但在发送参数和使用 POST 方法时我有点困惑。如果JsonObjectRequest
我可以发送方法类型、url、参数,但JsonArrayRequest
如何发送用户名和密码等参数并使用 POST 方法。请帮帮我。这是我写的代码。
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 数据是这样的:
{
"username":"rushi",
"firstname": "abc",
},
{
"username": "xyz",
"firstname": "vxa",
},
参数未发送。请帮我解决这个问题。先感谢您。