1

我正在开发 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);
                    }
                });

日志猫:

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'."}
4

1 回答 1

0

问题出在 web 服务中的 bodyparameter 上。它接受整体作为 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);
                        }
                    });

它现在正在工作。

于 2015-03-24T13:22:37.100 回答