android-volley icon indicating copy to clipboard operation
android-volley copied to clipboard

POST JsonObject and get JsonArray response

Open pishguy opened this issue 8 years ago • 4 comments

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

pishguy avatar Sep 26 '15 20:09 pishguy

In your case you have to implement your custom Request because it is not possible for Volley to provide endless combinations of different Request/Response pair.

ayltai avatar Oct 04 '15 15:10 ayltai

Thanks sir, but i can resolve this problem

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);
    }
}

pishguy avatar Oct 04 '15 15:10 pishguy

Is your token on the header of post ?

yigityuksel avatar Nov 07 '15 06:11 yigityuksel

Thank you Sir I am Getting error at Listeners.ServerResponseListener and tell me what i need to pass here

rangamramesh avatar Jul 14 '17 12:07 rangamramesh