你的位置:首页 > 操作系统

[操作系统](iOS


总结一下AFNetworking的基本使用方法。

①在AppDelegate.h中,导入AFHTTPRequestOperationManager.h文件,添加一个属性manager: 

@property (strong,nonatomic(AFHTTPRequestOperationManager *)manager;

  在实现文件中Application:didFinishLauchingWithOpertions:中完成manager的初始化:

self.manager = [AFHTTPRequestOperationManager manager];

②在需要使用HTTP协议加载数据的地方,导入AppDelegate.h,实现以下语句:

appDelegate = [UIApplication sharedApplication].delegate;
NSString *str_input_data = [NSString stringWithFormat:@"238549_21963$2053$11$%@$%@$%@$%@$None$%@$O10.001$%@$%@$%@$%@$%@",ageCode,sexCode,nationCode,marryCode,symCode,self.highBPField.text,self.lowBPField.text,self.rateField.text,self.temperatureField.text,self.breathField.text];NSString *temStr = [NSString stringWithFormat:@"http://url?str_input_data=%@",str_input_data];//str_input_data为服务端需要参数的接口NSString *encoded = [temStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//需要进行UTF-8编码转换才能是能被识别的URL[appDelegate.manager GET:encoded parameters:nil success:^(AFHTTPRequestOperation *operation, id resonseObject){
  //这里面对接收到的responseObject进行处理,它已经是解析好的json文件了  resultKeys = [NSMutableArray array];  resultValues = [NSMutableArray array];  NSArray *array = resonseObject;  for (NSInteger j = 0; j < array.count; j ++){    if (j % 2 == 0) {      [resultKeys addObject:[array objectAtIndex:j]];    }    else if (j % 2 == 1 && ![(NSString *)array[j] isEqual:@" 0"]){      //[resultValues addObject:[array objectAtIndex:j]];      [resultValues addObject:(NSString *)array[j]];    }    else{      break;    }  }  NSLog(@"keys=%@,value= %@",resultKeys,resultValues);  [self.resultTable reloadData];}failure:^(AFHTTPRequestOperation *operation, NSError *error){ NSLog(@"连接诊断结果服务错误:%@",error);}];