4

in volley we have some ability to retrieve data from server such as jsonObject,jsonArray and String. in this below sample we can get simply jsonObject or jsonArray response from server,

public static void POST(HashMap<String, String> params, final Listeners.ServerResponseListener listener) {
 JsonObjectRequest req1 = new JsonObjectRequest(ApplicationController.URL, new JSONObject(params),
 new Response.Listener<JSONObject>() {
 @Override
 public void onResponse(JSONObject response) {
 Log.e("Response:", response.toString());
 if (listener != null)
 listener.onResultJsonObject(response);
 else
 Log.e(TAG,"Error: SetServerResponse interface not set");
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 Log.e("Error: ", error.getMessage());
 }
 });
 ApplicationController.getInstance().addToRequestQueue(req1);
}

my problem is i want to send jsonObject from this method and get jsonArray or jsonObject from server, and i can not get simply array from server with this method. for example i must be filter server response with this jsonObject:

HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "AbCdEfGh123456");
params.put("search_count", "10");
params.put("order_by", "id");

server return jsonArray and i can not get that with Volley response

asked Sep 26, 2015 at 19:16
2
  • Volley also supports JsonArrayRequest.. Commented Sep 26, 2015 at 19:21
  • @akash93 yes thats right, how to post OBJECT? i dont have any json array such as parameters Commented Sep 26, 2015 at 19:25

2 Answers 2

4

Looking at the source code of JsonArrayRequest. There is a constructor which takes in a JSONObject. You should check it out

answered Sep 26, 2015 at 19:36
Sign up to request clarification or add additional context in comments.

3 Comments

this is not my answer sir. my problem is send json object such as simple request from server with Volley POST method and get JsonArray response.
that is exactly what this does... You can use it to send a JSONObject as POST params whilst expecting a JSONArray as response.
I tried your method and first it gave me an error for not recognizing the constructor. Apparently volley version 1.0.0 does not have it. I had to switch in gradle to compile 'com.mcxiaoke.volley:library:1.0.19' in order to have it. Thanks for the info.
1
public class RetreiveData {
 public static final String TAG = RetreiveData.class.getSimpleName();
 public static void POST(String localhost, final HashMap<String, String> params, final Listeners.ServerResponseListener listener) {
 StringRequest post = new StringRequest(Request.Method.POST, localhost, new Response.Listener<String>() {
 @Override
 public void onResponse(String response) {
 try {
 if (listener != null)
 listener.onResponse(response.toString());
 else
 Log.e(TAG, "Error: SetServerResponse interface not set");
 } catch (Exception e) {
 e.printStackTrace();
 Log.d("Error: ", e.getMessage());
 }
 }
 }, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
 Log.d("Error: ", error.toString());
 }
 }) {
 @Override
 protected Map<String, String> getParams() throws AuthFailureError {
 Map<String, String> map = params;
 return map;
 }
 @Override
 public RetryPolicy getRetryPolicy() {
 setRetryPolicy(new DefaultRetryPolicy(
 5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
 return super.getRetryPolicy();
 }
 };
 ApplicationController.getInstance().addToRequestQueue(post);
 }
}
answered Oct 8, 2015 at 8:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.