Problem in sending parameters to get json array in Volley


Hi, in my android application i want to use Vollley to get json array served by a php web page. The problem that i am facing now is i cannot pass parameters to the php page. It seems tha the getParams() method is not called at all. Anyone know how to solve the problem? Please let me know. Thank a lot.

By trum2012 asked on 2015-08-17
answer to this question

yuk.dara
By yuk.dara

I guess you are using JsonArrayRequest. I faced the same problem when i started to use Volley in my android project. I solved the problem by using StringRequest instead of JsonArrayRequest. The example code below might help you.
public void onResponse(String response){
// Successfully download json
// So parse it
try{
JSONArray jsonArray=new JSONArray(response);
for(int i=0;i<jsonArray.length();i++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// do sth
} catch (JSONException e) {
e.printStackTrace();
}

}
}catch (JSONException e){

}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("Error", "Error in fetching data");
}
}){
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("param1","param1");
params.put("param2", "param2");
return params;
}
};
// add json array request to the request queue
requestQueue.add(jsonArrayRequest);


2015-08-17 Reply





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.