【问题标题】:no post using ion library没有使用离子库的帖子
【发布时间】:2014-09-27 16:52:08
【问题描述】:

我正在尝试使用以下代码在本地网络中的计算机上通过 android ion 库发布一个 json 对象:

String url = "http://192.168.1.23/ws/svn/branches/application/public/connectMobile";
                    try {


                        JsonObject json = new JsonObject();
                        json.addProperty("email", email.getText().toString());
                        json.addProperty("psw", password.getText().toString());
                        Log.i("json envoyé",json.toString());



                            Ion.with(context)
                                    .load("POST",url)
                                    .addHeader("Content-Type", "application/json")
                                    .setLogging("ion-geny", Log.DEBUG)
                                    .setJsonObjectBody(json)
                                    .asJsonObject()
                                    .setCallback(new FutureCallback<JsonObject>() {
                                        @Override
                                        public void onCompleted(Exception e, JsonObject result) {
                                            if (result != null)
                                            {
                                                Log.i("result",result.toString());
                                            }
                                            else
                                            {
                                                Log.i("result","null");
                                            }
                                            if (e != null)
                                            {
                                                Log.i("error",e.toString());

                                            }
                                            else
                                            {
                                                Log.i("error","null");
                                            }

                                        }

                                    });



                    } catch (Exception ex) {
                        ex.printStackTrace();
                        Log.i("except", ex.toString());
                    }

调试给出了这个:

09-27 12:19:43.302 3309-3309/com.bewizme.bewizmeloginsample 我/邮箱:f@f.com 09-27 12:19:43.302 3309-3309/com.bewizme.bewizmeloginsample I/密码:f 09-27 12:19:43.362 3309-3309/com.bewizme.bewizmeloginsample I/connected﹕已连接 true 09-27 12:19:43.402 3309-3309/com.bewizme.bewizmeloginsample D/dalvikvm:GC_FOR_ALLOC 释放 142K,7% 释放 2917K/3116K,暂停 15ms, 总共 18 毫秒 09-27 12:19:43.482 3309-3309/com.bewizme.bewizmeloginsample I/json envoyé: {"email":"f@f.com","psw":"f"} 09-27 12:19:43.602 3309-3309/com.bewizme.bewizmeloginsample D/dalvikvm:GC_FOR_ALLOC 释放 154K,6% 释放 3275K/3480K,暂停 14ms, 总共 15 毫秒 09-27 12:19:43.622 3309-3309/com.bewizme.bewizmeloginsample D/ion-geny: (0 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile: 准备请求 09-27 12:19:43.632 3309-3309/com.bewizme.bewizmeloginsample I/ion-geny: (0 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile: 使用加载器:com.koushikdutta.ion.loader.HttpLoader@b20a3808 09-27 12:19:43.652 3309-3331/com.bewizme.bewizmeloginsample D/ion-geny: (0 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile: 执行请求。 09-27 12:19:44.862 3309-3331/com.bewizme.bewizmeloginsample D/ion-geny: (1212 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile: 响应不可缓存 09-27 12:19:44.902 3309-3331/com.bewizme.bewizmeloginsample D/ion-geny: (1249 ms) http://192.168.1.23/ws/svn/branches/application/public/connectMobile: 连接成功 09-27 12:19:45.002 3309-3331/com.bewizme.bewizmeloginsample D/dalvikvm:GC_FOR_ALLOC 释放 263K,9% 释放 3462K/3788K,暂停 61ms, 总共 61 毫秒 09-27 12:19:45.052 3309-3309/com.bewizme.bewizmeloginsample I/结果:null 09-27 12:19:45.052 3309-3309/com.bewizme.bewizmeloginsample I/error: com.google.gson.JsonParseException: 无法解析 json

我怀疑解析错误是由于没有返回任何内容。

此外,在我的服务器上,我编写了一段代码来检查传入的内容,但我在服务器上没有从我的示例中收到任何内容,但是当尝试使用 RESTEASY 和 firefox 发送相同内容时,我的服务器接收到的帖子正确发送帖子和回复被发回。

有什么想法吗?我不知道怎么了...

【问题讨论】:

  • 您能否调试您的应用程序,检查响应,并检查here JSON 是否有效?
  • json 发送是正确的:{"email":"f@f.com","psw":"f"} 服务器端什么都没有捕获(就像帖子永远不会到达)然后响应为空,我怀疑这很好,因为服务器从未收到,所以从未回答)
  • 我认为 Gson 可以很好地处理 null 和空字符串。所以我也认为格式问题来自服务器端......但我无法确定是什么。
  • JsonArray可能有问题,检查你的服务器响应是JsonObject还是JsonArray?

标签: android post android-ion


【解决方案1】:

如果您因为 json 解析错误而陷入困境,因为当您将结果输入 JsonObject 格式并尝试获取字符串时,通常人们会编写错误的解析,所以我现在用字符串格式编写它,您无需解析它,您可以在字符串,因此您可以使用 .toString 轻松获取字符串,并且有很多机制可以解决此问题,并且有很多编写代码的方法,但含义相同,但有时您编写的代码相同且含义也相同,但编译器无法翻译它。所以我们得到错误这就是为什么我在这里写字符串现在你可以得到你的回应。

  .addHeader("Content-Type", "application/json")
                                .setLogging("ion-geny", Log.DEBUG)
                                .setJsonObjectBody(jsobj.getAsJsonObject())
                            .asString()
                                .setCallback(new FutureCallback<String>() {
                                    @Override
                                    public void onCompleted(Exception e, String result) {
                                        if (result != null)
                                        {
                                            Log.i("result",result.toString());

【讨论】:

  • 如果您不仅要删除一些代码,而且还要解释他和您的代码中发生的事情,那就太好了。它可以帮助问题作者和其他用户。如果它有效就很好,但在我看来,知道为什么更重要。
  • 如果您在 json 解析错误方面遇到困难,因为当您将结果输入 JsonObject 格式并尝试获取字符串时,通常人们会编写错误的解析,所以我现在用字符串格式编写它,您无需解析它可以在 String 中获得您的响应,因此您可以使用 .toString 轻松获取 String,并且有很多机制可以解决这个问题,并且有很多方法可以编写代码,但含义相同,但有时您编写的代码相同,含义也相同,但编译器可以不翻译它。所以我们得到错误这就是为什么我在这里写字符串现在你可以得到你的回应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
相关资源
最近更新 更多