Buy*_*ian 3 objective-c ios afnetworking
我在这里遇到一个奇怪的问题,并且我没有发现任何其他人有同样的问题.
我正在AFNetworking做一个AFJSONRequestOperation.
它在第一次建立网络时起作用.但是,一旦建立网络连接,相同的代码就会失败,并显示"错误的URL"错误.
奇怪的是,应用程序甚至在失败之前都没有ping服务器,我正在使用Charles来嗅探所有请求.
还有其他人经历过这个吗?
供参考,以下是代码:
NSURL *url = [NSURL URLWithString:JOIN_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
// httpClient.parameterEncoding = AFJSONParameterEncoding;
NSString *path = [NSString stringWithFormat:@"%@?%@",JOIN_URL, getString];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:path parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"SUCCESS JSON: %@", JSON);
NSLog(@"RESPONSE URL: %@",response.URL);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"FAIL JSON: %@", JSON);
NSLog(@"FAIL ERROR: %@", error.description);
NSLog(@"RESPONSE URL: %@",response.URL);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error" message:@"Cannot connect now, please try again" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
[alert show];
}];
[operation start];
Run Code Online (Sandbox Code Playgroud)
您应该path像这样对URL 字符串进行编码:
NSString* escapedUrlString =[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Run Code Online (Sandbox Code Playgroud)
据我了解AFHTTPClient,你为他提供了baseURL这是基本 URL指定的所有路径,这将被追加.然后,当您提供路径时,您只提供此路径的相对部分.
所以,如果你有一个WebService http://www.example.com/webservice/其中有像一些方法/listAll?n=10,例如,你将只提供"listAll"给path的参数requestWithMethod:path:parameters:和字典@{ @"n" : @10 }的parameters说法.
无论如何,你已经提供了JOIN_URL你的实例AFHTTPClient,所以如果你JOIN_URL再次在路径中传递它,它将在AFHTTPClient内部构建的URL中出现两次!
| 归档时间: |
|
| 查看次数: |
3305 次 |
| 最近记录: |