Search code examples
androidjsonandroid-ionion-koush

Ion POST as url parameters and not JSON body Android


I want to POST to my server API using parameters instead of JSON body.

I can implement this:

JsonObject json = new JsonObject();
                json.addProperty("u_id", "XXX");
                json.addProperty("emp_id", "XXX");
                json.addProperty("lat", Double.toString(lat));
                json.addProperty("lon", Double.toString(lon));
                json.addProperty("paper", "5");
                json.addProperty("plastic", "10");
                json.addProperty("mode", "cash");
                json.addProperty("status", "init");

                Ion.with(getApplicationContext())
                        .load(getString(R.string.url)+"/transaction")
                        .setJsonObjectBody(json)
                        .asJsonObject()
                        .setCallback(new FutureCallback<JsonObject>() {
                            @Override
                            public void onCompleted(Exception e, JsonObject result) {
                                if (e != null) {
                                    Toast.makeText(getBaseContext(), "Data : " + e.getStackTrace(), Toast.LENGTH_LONG).show();
                                } else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            progress.dismiss();
                                        }
                                    });
                                    Toast.makeText(getBaseContext(), "Pickup added successfully! We will contact you soon.", Toast.LENGTH_LONG).show();
                                }
                            }
                        });

But I want to achieve the same by POST ing to http://someserver.com/transaction/<u_id>/<emp_id>/22.56/88.45/5/10/cash/init where my server is ready to handle the parameters.

Anyway, Koushik Dutta (@koush) the library is wonderful and asynchronous. Loved it.


Solution

  • That's possible, but only if you manually construct the request url.

    The request data of a POST request should go in the request body, done with setJsonObjectBody(). The url is merely the endpoint where the POST request should be sent to.

    Analogy
    If http://someserver.com/transaction is the name of the city you live in, /<u_id>/<emp_id>/22.56/88.45/5/10/cash/init is the address of your house, and json is the package that is delivered to your house.

    You can specify the address where your package should be sent to, but you don't put information about the content of the package on the address label. These are 2 seperate types of data and should be treated differently.

    Therefore you should do something like this

    JsonObject json = new JsonObject();
    json.addProperty("lat", Double.toString(lat));
    json.addProperty("lon", Double.toString(lon));
    json.addProperty("paper", "5");
    json.addProperty("plastic", "10");
    json.addProperty("mode", "cash");
    json.addProperty("status", "init");
    
    String u_id = "XXX";
    String emp_id = "XXX";
    String url = getString(R.string.url) + "/transaction/" + u_id + "/" +
                 emp_id + "/22.56/88.45/5/10/cash/init";
    
    Ion.with(getApplicationContext())
            .load(url)
            .setJsonObjectBody(json)
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
    
                }
            });
    

    Note that the slashes in the url may need to be escaped. Don't know from the top of my head.