【问题标题】:Ion POST as url parameters and not JSON body AndroidIon POST 作为 url 参数而不是 JSON 正文 Android
【发布时间】:2015-09-26 14:29:44
【问题描述】:

我想使用参数而不是 JSON 正文 POST 到我的服务器 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/&lt;u_id&gt;/&lt;emp_id&gt;/22.56/88.45/5/10/cash/init 来实现相同的目的,我的服务器已准备好处理参数。

无论如何,Koushik Dutta (@koush) 库非常棒且异步。很喜欢。

【问题讨论】:

    标签: android json android-ion ion-koush


    【解决方案1】:

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

    POST 请求的请求数据应该放在请求正文中,用setJsonObjectBody() 完成。 url 只是 POST 请求应该发送到的端点。

    类比
    如果http://someserver.com/transaction 是您居住的城市名称,/&lt;u_id&gt;/&lt;emp_id&gt;/22.56/88.45/5/10/cash/init 是您家的地址,json 是送到您家的包裹。

    您可以指定应将包裹寄往的地址,但不要在地址标签上注明包裹内容信息。这是两种不同类型的数据,应区别对待。

    因此你应该这样做

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-04
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多