我正在研究android web应用程序。 当我使用简单的HttpPost方法调用Web服务时。 其中url是
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回购中看过很多问题。并尝试了几乎解决方案,但仍然没有解决错误。
这里有什么问题?
更新后的代码:
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'."}
答案 0 :(得分:0)
问题在于webservice中的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);
}
});
它现在正在运作。