ios - 如何将AFHTTPClient,Afnetworking 1.0迁移到2.0

标签 ios xcode5 afnetworking afnetworking-2 afhttpclient

我的问题是我有一个旧代码,我不知道如何更改它。
我有1个叫做API(AFHTTPClient)的类,我有2种方法的问题,因为我不知道如何将它们放在2.0中:
这个:

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock
{
NSMutableURLRequest *apiRequest = 
[self multipartFormRequestWithMethod:@"POST" 
path:kAPIPath 
parameters:params 
constructingBodyWithBlock: ^(id formData) {
//TODO: attach file if needed
}];

AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
}


and this:


#pragma mark - init
//intialize the API class with the destination host name

-(API*)init
{
//call super init
self = [super init];

if (self != nil) {
    //initialize the object
    user = nil;

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

    // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
    [self setDefaultHeader:@"Accept" value:@"application/json"];
}

return self;
}

我做了一个名为Api的新类,现在(AFHTTPRequestOperationManager)好吗?
我尝试这样做,但我不知道
-(API*)init
{//call super init
self = [super init];

//initialize the object
if (self != nil) {
    //initialize the object
    user = nil;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    AFJSONRequestSerializer *a=[AFJSONRequestSerializer serializer];


    [a setValue:@"Accept" forHTTPHeaderField:@"application/json"];
    [a setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [a setValue:@"content-Type" forHTTPHeaderField:@"text/html"];
    [a setValue : @ "text / html; charset = UTF-8" forHTTPHeaderField : @ "Content-Type" ];

}
return self;
}

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock
{

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = params;
NSURL *filePath = [NSURL URLWithString:@"http://162.243.199.147/mujeresquecorren/iReporter/index.php"];
[manager POST:@"api" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  [formData appendPartWithFileURL:filePath name:@"api" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
 NSMutableURLRequest *apiRequest = [NSMutableURLRequest requestWithURL:filePath];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
// operationManagerInstance.requestSerializer = requestSerializer;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:apiRequest];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"http://162.243.199.147/mujeresquecorren/iReporter/index.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:nil];
// AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//success!
completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failure :(
completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
}

这是错误之一:

rror:错误域= NSURLErrorDomain代码= -1002“不支持的URL” UserInfo = 0xa64f980 {NSErrorFailingURLStringKey = api,NSErrorFailingURLKey = api,NSLocalizedDescription =不支持的URL,NSUnderlyingError = 0xa782e10“不支持的URL”}

请帮助,即时通讯对此感到疯狂,我需要我的代码可以在我的应用程序中正常工作!
非常感谢!!!!

最佳答案

错误消息中全都有。 “api”不是有效的网址。您需要指定一个绝对URL,或者使用baseURLinitWithBaseURL:初始化您的请求操作管理器。

关于ios - 如何将AFHTTPClient,Afnetworking 1.0迁移到2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20792044/

相关文章:

ios - AFNetworking CFData 泄漏?

ios - swift 中的统一响应/错误处理

ios - 如何将参数传递给 NSNotificationCenter 中的选择器?

ios - 不能只用名称填充 TableView

Xcode 5 不会导入配置文件

ios - 我如何在核心数据中获得 SUM

ios后台下载完成但 View 已被破坏

ios - UIAlertView 不是每次都弹出

ios - 无法读取数据,因为它的格式不正确 [swift 3]

FB登录后IOS切换 View