Reputation: 23
i have array look like this
arrayname[{"code" : "abc","code2":"cba",}]
i want send it as paramaters,put it in getParam() function with volley to my server
the problem look similiar to this
Volley pass array as parameters
any help how to do it?
thanks in advance
Upvotes: 2
Views: 4273
Reputation: 3622
For this, you have to make JsonArrayRequest. In Volley there is JsonArrayRequest class use that one for JsonArray request.
This is the method available in JsonArrayRequest class.
public JsonArrayRequest(int method, String url, JSONArray jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
}
May be this will help you:
JSONArray jArrayInput=new JSONArray();
JSONObject jObjectInput=new JSONObject();
jObjectInput.put("code", abc);
jObjectInput.put("code2", cba);
jArrayInput.put(jObjectInput);
JsonArrayRequest request = new JsonArrayRequest(Method.POST, /*Your base url*/, jArrayInput , new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Here success response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Here error response
}
});
MyVolley.getRequestQueue().add(request);
Upvotes: 0
Reputation: 7437
Here is a method in an app I made that does what you're trying to do. (I think?)
My problem was that I had multiple post parameters that I wanted to send, but one of them was an array.
For example:
http://www.yourDB.com/getData.php?param1=blah¶m2=blah¶m3=[ [array] ].
So... to make sure that the PHP page understands that I'm sending an array, I add data to the array as follows (notice the ' [] ' following the param).
postParams.add(new String[]{"param3[]", itemName});
So... the server sees this:
http://www.yourDB.com/getData.php?param1=blah¶m2=blah¶m3[]=item1¶m3[]=item2¶m3[]=item3...
The key to understand is this part:
"@Override
public byte[] getBody() throws AuthFailureError {"
The trick is that you send an ArrayList of String arrays into the method and override the getBody() part like this:
private void Url_2_Adapter(String url, final ArrayList<String[]> postParams) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// do something with response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Log.i(TAG, "WE HAD ERROR " + error.toString());
try {
Toast.makeText(getActivity(), "Server says: Error code " + error.networkResponse.statusCode + "\nIf this continues, please report it! Thanks.", Toast.LENGTH_LONG).show();
}
catch (Exception e){
}
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
StringBuilder result = new StringBuilder();
boolean first = true;
for (String[] entry : postParams)
{
if (first) {
first = false;
} else {
result.append("&");
}
try {
result.append(URLEncoder.encode(entry[0], "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry[1], "UTF-8"));
} catch (UnsupportedEncodingException e) {
// this basically will never happen :)
}
}
return result.toString().getBytes();
}
};
Z_VolleySingleton.getInstance().getRequestQueue().add(stringRequest);
}
Upvotes: 1
Reputation: 567
I didn't worked on the Volley Library , I used AsyncHttpClient : The logic you can apply here like this:
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("code", abc);
jsonObject.put("code2", cba);
jsonArray.put(jsonObject);// this you need to pass using volley library
Upvotes: 0