How To Send Array Of Params Using Volley In Android
I am developing an application which send lot of data to server.Now i want to send an array of params to a php page using volley.But i am not able to send it. Code for adding param
Solution 1:
Use
HashMap<String ,String> params=new HashMap<String, String>(7);
for(int i=1;i<=7;i++)
{
params.put("params_"+i, arr[i]);
}
in CustomJobjectRequest
class because currently you are using String
type as value in Map in CustomJobjectRequest
class but sending String[]
type when create object of CustomJobjectRequest
class.
Edit:
To send all values in single parameter to server use JSONObject
.Create a json object using all key-value as:
JSONObject jsonObject=newJSONObject();
for(int i=1;i<=7;i++)
{
arr[i]="questionId_"+i+"_"+"ans_"+i;
jsonObject.put("params_"+i,arr[i]);
}
HashMap<String ,String> params=newHashMap<String, String>();
params.put("params",jsonObject.toString());
TO send all values on server side get params
and convert to JSON object and iterate to get all values
Solution 2:
Use
Map<String, String> postParam = new HashMap<>();
int i=0;
for(Stringobject: friendIds){
postParam.put("friendIds["+(i++)+"]", object);
// you first send both data with same param name as friendnr[] .... now send with params friendnr[0],friendnr[1] ..and so on
}
this work for me, hope it work to you.
Solution 3:
Use google json Library to make json array.
compile'com.google.code.gson:gson:2.6.2'
And this code is to put json array in the body of the request
privatevoidsendTokenToServer(final String jsonArrayString) {
String tag_string_req = "string_req";
String url = Const.SEND_TOKEN_TO_SERVER;
final StringRequest strReq = newStringRequest(Request.Method.POST,
url, newResponse.Listener<String>() {
@OverridepublicvoidonResponse(String response) {
Log.d(TAG, response.toString());
hideProgressDialog();
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
@OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = newHashMap<>();
header.put("Content-Type", "application/json; charset=utf-8");
return header;
}
@OverridepublicStringgetBodyContentType() {
return"application/json; charset=utf-8";
}
@Overridepublic byte[] getBody() throws AuthFailureError {
try {
return jsonArrayString.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
returnnull;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
Solution 4:
Step 1
Make json param
Map<String, String> jsonParams = newHashMap<>();
Step 2
Make set or Arraylist why set or arraylist because no need to put fixed lenth
private Set<String> arr;
for(int i=1;i<=7;i++)
{
arr[i]="questionId_"+i+"_"+"ans_"+i;
arr.add("params_"+i,arr[i]);
}
Step 3 pass set object as a string
if (arr!= null) {
jsonParams.put("param", arr.toString());
}
Post a Comment for "How To Send Array Of Params Using Volley In Android"