繁体   English   中英

离子POST作为网址参数而不是JSON正文

[英]Ion POST as url parameters and not JSON body Android

我想使用参数而不是JSON正文发布到服务器API。

我可以实现这一点:

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

但是我想通过POST到达http://someserver.com/transaction/<u_id>/<emp_id>/22.56/88.45/5/10/cash/init来实现相同的目的,其中我的服务器已准备好处理参数。

无论如何,Koushik Dutta(@koush)的库是很棒且异步的。 爱它。

这是可行的,但前提是您手动构造了请求url。

POST请求的请求数据应该进入请求主体,由setJsonObjectBody() url只是POST请求应发送到的端点。

比喻
如果http://someserver.com/transaction是您居住的城市的名称,则/<u_id>/<emp_id>/22.56/88.45/5/10/cash/init是您的房子的地址,而json是送到您家的包裹。

您可以指定将包裹发送到的地址,但不要在地址标签上放置有关包裹内容的信息。 这些是2种单独的数据类型,应区别对待。

因此,你应该做这样的事情

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) {

            }
        });

请注意,URL中的斜杠可能需要转义。 从我的头上不知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM