I need to make a request with a JSONObject as follows:
{
\'LangIDs\': [1, 2],
\'GenreIDs\': [4],
\'LowPrice\': 0,
\'HighPrice\': 999,
\'S
Use following class for passing json object as paramater:
public class VolleyJSonRequest {
public void MakeJsonRequest(final String Tag, String url, final ArrayList list, final ResponceLisnter responceLisnter, final String HeaderKey) {
Map params = new HashMap();
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
//MyLog.ShowLog(list.get(i).getKey(), list.get(i).getValue());
params.put(list.get(i).getKey(), list.get(i).getValue());
}
}
JSONObject obj = new JSONObject(params);
JsonArrayRequest jsObjRequest = new JsonArrayRequest
(Request.Method.POST, url, obj, new Response.Listener() {
@Override
public void onResponse(JSONArray response) {
try {
// Iterator x = response.keys();
// JSONArray jsonArray = new JSONArray();
//
// while (x.hasNext()){
// String key = (String) x.next();
// jsonArray.put(response.get(key));
// }
responceLisnter.getResponce(response.toString(), Tag);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(com.android.volley.VolleyError error) {
// TODO Auto-generated method stub
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
responceLisnter.getResponceError(VolleyError.TIMEOUT_ERROR, Tag);
} else if (error instanceof AuthFailureError) {
responceLisnter.getResponceError(VolleyError.AUTH_ERROR, Tag);
} else if (error instanceof ServerError) {
error.printStackTrace();
responceLisnter.getResponceError(VolleyError.SERVER_ERROR, Tag);
} else if (error instanceof NetworkError) {
responceLisnter.getResponceError(VolleyError.NETWORK_ERROR, Tag);
}
}
}) {
/**
* Passing some request headers
* */
@Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Content-Type", HeaderKey);
return headers;
}
};
;
TestVolleyJson.getInstance().getRequestQueue().add(jsObjRequest);
jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(50000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
}
In your Application class:
public class TestVolleyJson extends Application {
private static TestVolleyJson mInstance;
public static Context context;
private RequestQueue mRequestQueue;
public static synchronized TestVolleyJson getInstance() {
return mInstance;
}
@Override
public void onCreate() {
super.onCreate();
context = this;
mInstance = this;
mRequestQueue = Volley.newRequestQueue(this);
}
public RequestQueue getRequestQueue() {
return mRequestQueue;
}
}
Make a requestModel:
public class RequestModel {
String Key;
String Value;
public ArrayList getJsonObjects() {
return jsonObjects;
}
public void setJsonObjects(ArrayList jsonObjects) {
this.jsonObjects = jsonObjects;
}
ArrayList jsonObjects;
public RequestModel(String key, String value) {
this.Key = key;
this.Value = value;
}
public RequestModel(String key, ArrayList jsonObjects) {
this.Key = key;
this.jsonObjects = jsonObjects;
}
public String getValue() {
return Value;
}
public void setValue(String value) {
Value = value;
}
public String getKey() {
return Key;
}
public void setKey(String key) {
Key = key;
}
}
and in your activity:
VolleyJSonRequest volleyJSonRequest = new VolleyJSonRequest();
ArrayList list = new ArrayList<>();
list.add(new RequestModel("Param1", "abc"));
list.add(new RequestModel("Param2", "xyz"));
volleyJSonRequest.MakeJsonRequest("Tag", "URL", list, responceLisnter, "application/json; charset=utf-8");
}
ResponceLisnter responceLisnter = new ResponceLisnter() {
@Override
public void getResponce(String str, String Tag) throws JSONException {
if (Tag.equalsIgnoreCase("Logintest")) {
Log.e(Tag, " Response is" + str);
}
}
@Override
public void getResponceError(String errorStr, String Tag) {
}
};
Make a responselistener interface:
public interface ResponceLisnter {
void getResponce(String str, String Tag) throws JSONException;
void getResponceError(String errorStr, String Tag);
}
Hope it will work for you