【问题标题】:issue with using Ion android networking library使用 Ion android 网络库的问题
【发布时间】:2015-03-23 13:04:04
【问题描述】:

我正在开发 android web 应用程序。 当我使用简单的 HttpPost 方法调用 Web 服务时。 网址是

http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039&folderName=issue&parentFolder=0

使用简单的http

String url = "http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?
userID=2039&folderName=issue&parentFolder=0"

HttpPost post = new HttpPost(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
responseText = EntityUtils.toString(entity);

这个 Http 方法一切正常。

但是当我尝试使用Ion android 库执行此操作时。使用下面的代码。

Ion.with(context)
                .load("POST", "http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder")
                .setLogging("LOG-POST",Log.VERBOSE)
                .setHeader("Content-Type","application/json")
                .setBodyParameter("userID","2039")
                .setBodyParameter("folderName","TEST post")
                .setBodyParameter("parentFolder","0")
                .asString()
                .setCallback(new FutureCallback<String>() {
                    @Override
                    public void onCompleted(Exception e, String result) {
                        if (e != null) {
                            Toast.makeText(context, "Error downloading file", Toast.LENGTH_LONG).show();
                            return;
                        }
                        Toast.makeText(context, "Download completed", Toast.LENGTH_LONG).show();
                        Log.d("RESULT",result);
                    }
                });

它返回以下消息。

{"Message":"No HTTP resource was found that matches the request URI 'http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder'."}

我在 Ion 的官方 github repo 上阅读了很多问题。并尝试了几乎解决方案,但仍然没有解决错误。

这里有什么问题?

更新代码:

JsonObject json = new JsonObject();
        json.addProperty("userID","2039");
        json.addProperty("folderName","temp0011");
        json.addProperty("parentFolder","0");

        Ion.with(context)
                .load("POST", "http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder")
                .setLogging("ion-geny",Log.DEBUG)
                .setHeader("Content-Type","application/json")
                .setHeader("Cache-Control","no-cache")
                .setJsonObjectBody(json)
                .asString()
                .setCallback(new FutureCallback<String>() {
                    @Override
                    public void onCompleted(Exception e, String result) {
                        if (e != null){
                            Log.e("Error Result", e.toString());
                        }
                        Log.d("Result", result);
                    }
                });

Logcat:

D/ion-geny﹕ (0 ms) http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039: preparing request
D/ion-geny﹕ (0 ms) http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039: preparing request
I/ion-geny﹕ (0 ms) http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039: Using loader: com.koushikdutta.ion.loader.HttpLoader@42656120
D/ion-geny﹕ (0 ms) http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039: Executing request.
D/ion-geny﹕ (6611 ms) http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039: Response is not cacheable
D/ion-geny﹕ (6615 ms) http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039: Connection successful
D/ion-geny﹕ (8592 ms) http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039: Recycling keep-alive socket

回应:

{"Message":"No HTTP resource was found that matches the request URI 'http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?userID=2039'."}

【问题讨论】:

  • 请求成功,您的服务器在您发布到的 URL 上返回 404 消息 json。
  • @koush 嗨,首先感谢您创建 Ion。这太棒了。在这里,我更新了我的代码并附加了 logcat 和服务器响应。我的代码有问题还是网络服务有问题?
  • 这可能是网络服务的问题,或者您调用或处理该请求的方式。
  • 解决了问题。发布答案。

标签: android web-services http android-ion


【解决方案1】:

问题出在 web 服务中的 body 参数上。它接受整体作为 QueryString。并且需要设置HttpHeader "Content-Length = 0"。下面是我修改后的代码。

     Ion.with(context)
                    .load("POST", "http://xxx.xx.xxx.xxx/api/UploadFile/InsertFolder?
userID=2039&folderName=temp0011&parentFolder=0")
                    .setLogging("ion-geny",Log.DEBUG)
                    .setHeader("Content-Length","0")
                    .asString()
                    .setCallback(new FutureCallback<String>() {
                        @Override
                        public void onCompleted(Exception e, String result) {
                            if (e != null) {
                                Log.e("Error Result", e.toString());
                            }
                            Log.d("Result", result);
                        }
                    });

它现在可以工作了。

【讨论】:

  • 您也可以执行 setStringBody("") 来设置该参数。
  • .setStringBody("userID=2039&folderName=temp0013&parentFolder=0")
  • 根据您的服务,它可能会解析 url 编码的参数或查询字符串。如果是 url 编码的参数,使用 setBodyParameter
猜你喜欢
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
相关资源
最近更新 更多