ios 使用 afnetworking 在 url 中为 GET 方法传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21178697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameter in url for GET method using afnetworking
提问by iPhone Guy
i have url in which query is executed.
我有执行查询的网址。
https://<BASE_URL>/<TENANT_URL>/?query=where UserName='abc'&companyId=&page=1&pageSize=25&filterResultByColumns=true
i am escaping the remaining part after tenant url like this,
我正在像这样在租户 url 之后逃避剩余的部分,
NSString *requestUrl = [[NSString stringWithFormat:@"%@/?query=where UserName='%@'&companyId=&page=1&pageSize=25&filterResultByColumns=true",<TENANT_URL>,userCredential.userName]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
requestUrl = [NSString stringWithFormat:@"%@/%@",baseurl,requestUrl];
Here is my GET request.
这是我的 GET 请求。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
serializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
manager.responseSerializer = serializer;
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSString *path = [NSString stringWithFormat:@"%@",URL];
[manager GET:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError* error = nil;
NSArray* json = [NSJSONSerialization
JSONObjectWithData:responseObject
options:kNilOptions
error:&error];
success(json);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
But i always getting a 400 Bad request error. I think problem is with "query=where ..". But i am not sure. How can i parse the URL. I tested with "POSTMAN" in Chrome. It works perfectly. But it throws me an error when i run the app.
但我总是收到 400 Bad request 错误。我认为问题在于“query=where ..”。但我不确定。我如何解析 URL。我在 Chrome 中用“邮递员”进行了测试。它完美地工作。但是当我运行应用程序时它会引发错误。
Error:
错误:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)" UserInfo=0xb7ac2b0 {NSErrorFailingURLKey=https://<BASE_URL>/<TENANT_URL>/?query=where UserName='abc'&companyId=&page=1&pageSize=25&filterResultByColumns=true&url=https%3A%2F%2F<BASE_URL>%2F%2F<TENANT_URL>%2F%3F?query=where%2DUserName%3D%27abc%27%26companyId%3D%26page%3D1%26pageSize%3D25%26filterResultByColumns%3Dtrue, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb7e6910> { URL: https://<BASE_URL>/<TENANT_URL>/?query=where UserName='abc'&companyId=&page=1&pageSize=25&filterResultByColumns=true&url=https%3A%2F%2F<BASE_URL>%2F%2F<TENANT_URL>%2F%3F?query=where%2DUserName%3D%27abc%27%26companyId%3D%26page%3D1%26pageSize%3D25%26filterResultByColumns%3Dtrue } { status code: 400, headers {
"Cache-Control" = private;
"Content-Length" = 0;
"Content-Type" = "text/html";
Date = "Fri, 17 Jan 2014 05:29:56 GMT";
Server = "Microsoft-HTTPAPI/2.0";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSLocalizedDescription=Request failed: bad request (400)}
回答by James Frost
It looks like there are a number of issues with the URL you're constructing, and the way you're passing (or not passing) parameters into AFNetworking. You don't need to construct your query string yourself, as AFNetworking will do that for you. As mentioned in my comment above, passing query=where UserName='abc'
as part of a URL seems like a bad idea. However, here's a quick example of how you'd call AFNetworking's GET
method if your URL was slightly different:
看起来您正在构建的 URL 以及您将参数传递(或不传递)到 AFNetworking 的方式存在许多问题。您不需要自己构建查询字符串,因为 AFNetworking 会为您完成。正如我在上面的评论中提到的,query=where UserName='abc'
作为 URL 的一部分传递似乎是一个坏主意。但是,GET
如果您的 URL 略有不同,这里有一个快速示例,说明如何调用 AFNetworking 的方法:
// URL format: https://<BASE_URL>/<TENANT_URL>/?username=abc&companyId=&page=1&pageSize=25&filterResultByColumns=true
NSURL *baseURL = [NSURL URLWithScheme:@"https" host:BASE_URL path:TENANT_URL];
[manager GET:[baseURL absoluteString]
parameters:@{ @"username": @"abc",
@"companyId": @"example",
@"page": @1,
@"pageSize": @25,
@"filterResultByColumns": @YES }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// handle success
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// handle failure
}];
If you pass your parameters into the GET method, AFNetworking will construct the query string for you.
如果您将参数传递给 GET 方法,AFNetworking 将为您构造查询字符串。