【问题标题】:How to migrate AFHTTPClient, Afnetworking 1.0 to 2.0如何迁移 AFHTTPClient、Afnetworking 1.0 到 2.0
【发布时间】:2014-01-14 12:50:32
【问题描述】:

我的问题是我有一个旧代码,我不知道如何更改它。 我有 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];
}

这是错误之一:

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

请帮忙,我要疯了,我需要我的代码在我的应用程序中工作! 非常感谢!!!!

【问题讨论】:

  • 原来AFNetworking的迁移指南有什么问题...?你读过AFNetworking v2.0 的文档了吗?
  • 嗨,holex,是的,我已经阅读了指南,但我不知道该怎么做
  • 我也阅读了该指南,当我已经拥有类似于@natjauskas 的 AFHTTPClient 的 AFNetworking 1.0 代码时,它并没有帮助。迁移指南中需要添加更好的示例。

标签: ios xcode5 afnetworking afnetworking-2 afhttpclient


【解决方案1】:

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

【讨论】:

  • 嗨@AaronBrager!我这样做了,我的错误已经改变。现在是:预期状态码(200-299)得到 500 !!:(!有什么想法吗?
  • 您的服务器出于某种原因拒绝了您的帖子。您很可能需要提供一些您没有提供的附加信息,例如访问令牌或内容类型标头。
  • 无论如何,这个错误应该作为一个单独的问题发布,并提供尽可能多的附加信息,因为它是一个单独的问题。
  • 好的!!它是真的!!对不起!!我写了一篇关于新问题的帖子!:-)
  • 我不知道为什么,因为我已经改变了一些东西,但是有了你的回答,我的代码现在可以工作了!!谢谢亚伦!
猜你喜欢
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多