I'm trying to send arraylist containing numbers as parameter in volley request and then parse it and display the values (in string) in toast. But I'm getting null in response. I would like to know where is the problem.
json response:
{
"got_members": [
"1",
"2"
]
}
public class SearchFragment extends Fragment{
Button checkme;
private static final String ADDMEM = "http://www.example.com/api/event/addmembers/", KEY_ADDMEM = "adding_members";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.searchfragment, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
checkme = (Button)view.findViewById(R.id.checkme);
checkme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkarraylist();
}
});
}
private void checkarraylist(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, ADDMEM,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response != null){
try {
JSONObject object = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}else {
Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
try {
JSONArray itemArray = new JSONObject(response).getJSONArray("got_members");
for (int i = 0; i < itemArray.length(); i++) {
String value = itemArray.getString(i);
Toast.makeText(getActivity(), "Result:" + value + "\n", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
ArrayList<String> strings = new ArrayList<String>();
strings.add("10");
strings.add("20");
strings.add("30");
Map<String, String> params = new HashMap<String, String>();
int i=0;
for(String object: strings){
params.put("adding_members["+(i++)+"]", object);
System.out.println(object);
}
/* String array[]=strings.toArray(new String[strings.size()]);
for(String k: array)
{
params.put("adding_members", k);
System.out.println(k);
} */
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
}
First in your Object in ArrayList: create JSONObjectmethod name as getJSONObject, like this
Aftrer Here is how I converted it,in my activity
Here i converted both type JSONObject and JSONArray.
After in
First get the response in JsonObject and then retreive the JsonArray or change the string request to JsonArray request
or simply use
JSONArray array = new JSONObject (response).getJSONArray("got_members");
try this instead of sending whole arraylist,if you are sending contact
You have to parse the JSONObject first . Then get the JSONArray . like this way ->
You need to add the following code alongwith your overriding getParams() function
Also you need to change this line
to
This may be the late response but as per response example this could help a bit