我即将AFNetworking从1.x 升级到3.x. 我的问题是AFHTTPSessionManager和之间有什么不同AFURLSessionManager?什么时候使用?哪一个支持XML和JSON请求和响应?谢谢
我是目标c和IOS的新手,我正在尝试将一些值发布到网址并尝试获取响应.
但我无法编译我的项目因为,我有这个错误
/ Users/Desktop/MyIOSApps/Chat System/Chat System/SignInVC.m:92:14:'AFHTTPSessionManager'没有可见的@interface声明选择器'POST:parameters:progress:success:failure:'
进口
#import "SignInVC.h"
#import <QuartzCore/QuartzCore.h>
#import "ApplicationEnvironmentURL.h"
#import "Reachability.h"
#import "AFNetworking/AFNetworking.h"
#import "MBProgressHUD/MBProgressHUD.h"
#import "AppDelegate.h"
Run Code Online (Sandbox Code Playgroud)
Objective-c代码
- (void)submitLoginRequest:(NSString *)email password:(NSString *)password {
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:email forKey:@"Email"];
[dict setValue:password forKey:@"Password"];
NSLog(@"Login dicxtionary : %@", dict);
MBProgressHUD *hud;
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.label.text = @"Please wait. Logging in...";
[hud showAnimated:YES];
// send user login data to hosting via AFHTTP …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我在其中使用会话管理器调用同步请求,在
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
Run Code Online (Sandbox Code Playgroud)
我正在使用全局
@property(strong,nonatomic)AFHTTPSessionManager *manager;
Run Code Online (Sandbox Code Playgroud)
并在 viewdidload 中分配
manager = [[AFHTTPSessionManager alloc] init];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager setResponseSerializer:responseSerializer];
Run Code Online (Sandbox Code Playgroud)
并且一组请求在一个循环中发生。我可以选择取消我正在做的所有请求
for (manager in self.arrayOfTasks) {
[manager invalidateSessionCancelingTasks:YES];
}
manager=nil;
Run Code Online (Sandbox Code Playgroud)
,但问题是在取消之后,这个请求也一次又一次地发生。它不是取消。有人可以指导我找到我出错的地方吗?
使用Alamofire 4.0和Swift 3.0这有效:
Alamofire.request("http://content.uplynk.com/player/assetinfo/ab19f0dc98dc4b7dbfcf88fa223a6c3b.json", method: .get).responseJSON {
(response) -> Void in
print("Success: \(response.result)")
}
Run Code Online (Sandbox Code Playgroud)
成功:成功
但是,当我尝试使用Sessionmanager以便我可以包含timeoutInterval时,我的请求总是失败
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 15
let alamofireManager = Alamofire.SessionManager(configuration: configuration)
alamofireManager.request("http://content.uplynk.com/player/assetinfo/ab19f0dc98dc4b7dbfcf88fa223a6c3b.json").validate().responseJSON {
response in
print("Success: \(response.result)")
print("Response String: \(response.result.value)")
}
Run Code Online (Sandbox Code Playgroud)
成功:失败
如果有人能帮助我指出正确的方向,将不胜感激.
ios ×2
afnetworking ×1
alamofire ×1
ipad ×1
json ×1
nsurlsession ×1
objective-c ×1
swift3 ×1
xml ×1