afnetworking中,规范json格式的代码---这样打印出来errormessage就非常的方便了,呵呵呵呵

+(void) jsonFormRequest:(NSString *)url param:(NSDictionary *)param fromBlock:(void (^)(id  formData))block success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject respondsToSelector:@selector(objectForKey:)]) {
            [MTTAFNetworkingClient handleRequest:(NSDictionary *)responseObject success:success failure:failure];
        }else
        {
            NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            [MTTAFNetworkingClient handleRequest:responseDictionary success:success failure:failure];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        BLOCK_SAFE_RUN(failure,error);
    }];
}
+(void) jsonFormPOSTRequest:(NSString *)url param:(NSDictionary *)param success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *fullPath = [NSString stringWithFormat:@"%@%@",DD_URL_BASE,url];
    
    [manager POST:fullPath parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@<------",string);
        [MTTAFNetworkingClient handleRequest:responseDictionary success:success failure:failure];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if([error.domain isEqualToString:NSURLErrorDomain])
            error = [NSError errorWithDomain:@"没有网络连接。" code:-100 userInfo:nil];
        BLOCK_SAFE_RUN(failure,error);
    }];
}

你可能感兴趣的:(mac,ios)