mcxiaoke / android-volley

DEPRECATED
4.29k stars 1.56k forks source link

POST JsonObject and get JsonArray response #104

Open pishguy opened 9 years ago

pishguy commented 9 years ago

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

ayltai commented 9 years ago

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.

pishguy commented 9 years ago

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);
    }
}
yigityuksel commented 9 years ago

Is your token on the header of post ?

rangamramesh commented 7 years ago

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