gpt4 book ai didi

android - 使用 Ion android 网络库的问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:29:18 27 4
gpt4 key购买 nike

我正在开发 Android 网络应用程序。当我使用简单的 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 执行此操作时安卓库。使用以下代码。

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 存储库上阅读了很多问题。并尝试了几乎解决方案,但仍然没有解决错误。

这里有什么问题吗?

更新代码:

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

最佳答案

问题出在网络服务中的主体参数上。它接受整个作为 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);
}
});

它现在正在工作。

关于android - 使用 Ion android 网络库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210988/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com