【问题标题】:How to post data using AFNetworking 2.0 with application/x-www-form-urlencoded HttpHeaderField如何使用 AFNetworking 2.0 和 application/x-www-form-urlencoded HttpHeaderField 发布数据
【发布时间】:2015-10-04 16:35:36
【问题描述】:

我要将 NSURLConnection 转换为 AFNetworking 2.0。当我使用 NSURLConnection 发布工作正常的数据时。但我不知道如何使用 AFNetworking 2.0 来做到这一点。

这里是 NSURLConnection 请求的代码片段。

#define API_URL @https://myurl.com//login.php"

#define LOGIN_POST_TYPE @"app_data={\"app_data\":{\"user\":\"%@\",\"password\":\"%@\"}}"
+(void) post:(NSString*)api AndPostData:(NSData*)postData AndCallback: (void (^)(id result, NSError *error))callback {

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:API_URL];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [request setTimeoutInterval:10.0];
    [request setHTTPBody:postData];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
    {
        NSLog(@"%@",response);
        if ([data length] >0 && connectionError == nil)
        {
            NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            callback([dataStr JSONValue], nil);
        }
        else
        {
            callback(nil, connectionError);
        }
    }];

}


+(void) callAPIWithType:(int)apiType withParams:(NSDictionary *)param andCallback:(void(^)(id result, NSError *error)) callback
{

NSString *strPost = [NSString stringWithFormat:LOGIN_POST_TYPE, param[@"email"], param[@"password"]];

NSLog(@"%@", post);

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    [self post:API_URL AndPostData:postData AndCallback:^(id result, NSError *error) {
         callback(result, error);
    }];
}

就像我说的,这段代码运行良好。但是如何将此代码转换为 AFNetworking 2.0? 我用 AFNetworking 做了很多尝试。但是所有的方法都不适合我。 我得到错误响应。

我该怎么做?

【问题讨论】:

    标签: ios objective-c json afnetworking nsurlconnection


    【解决方案1】:

    这是我使用的代码示例,但根据您的要求进行了更改,您的 postData 只需为 NSDictionary 而不是 NSData...

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
    
    [manager POST:@"https://myurl.com//login.php" parameters:postData success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
      // responseObject contains the result
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"%@ failed: %@",NSStringFromClass([self class]),operation.responseObject);
    
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多