CocoaPods 安装教程很多 给个地址http://code4app.com/article/cocoapods-install-usage
//CocoaPods 加载AFNetworking 也可以下载下来在安装这种方法更新比较方便项目看起来没那么复杂
platform :ios, ‘7.0’
pod “AFNetworking”, “~> 2.0”
AFNetWoking github地址
get 请求
-(void)getMyExam:(NSString *)userNum withUrl:(NSString *)url
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//设置相应内容类型.
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/xml"];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//设置get参数
NSDictionary *parameters = @{@"userNum":userNum};
[manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *urlString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSError *error = nil;
GDataXMLDocument* XMLdocument = [[GDataXMLDocument alloc] initWithXMLString:urlString error:&error];
if (XMLdocument == nil) { return ; }
GDataXMLElement *root = [XMLdocument rootElement];
_myExamArray = [[NSMutableArray alloc] init];
_examArray = [[NSMutableArray alloc] init];
//获取根节点下的所以节点内容
NSArray *array = [root children];
for (int i = 0; i < [array count]; i++) {
GDataXMLElement *elm = [array objectAtIndex:i];
//打印节点的标签和所带字符
NSLog(@"ele name-->%@, ele string--->%@",[elm name],[elm stringValue]);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
post 获取内容
-(void)getArticle:(NSString )topic withPage:(NSInteger)page withUrl:(NSString )url
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@”application/xml”];//设置相应内容类型
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
NSString *newPage = [NSString stringWithFormat: @”%ld”,(long)page];
//post 参数
NSDictionary *parameters = @{@”topic”:topic,@”page”:newPage};
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *topicString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSError *error = nil;
GDataXMLDocument* XMLdocument = [[GDataXMLDocument alloc] initWithXMLString:topicString error:&error];
if (XMLdocument == nil) { return ; }
GDataXMLElement *root = [XMLdocument rootElement];
NSArray *array = [root children];
for (int i = 0; i < [array count]; i++) {
model = [[DynamicModel alloc] init];
GDataXMLElement *elm = [array objectAtIndex:i];
model.userNum = [[[elm elementsForName:@”userNum”]objectAtIndex:0] stringValue];
…………………………………………………………………………………………
model.userSchool = [[[elm elementsForName:@”userSchool”]objectAtIndex:0] stringValue];
model.articalTime = [[[elm elementsForName:@”articalTime”]objectAtIndex:0] stringValue];
[articleArray addObject:model];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@”Error: %@”, error);
}];
}
post 请求 上传头像
-(void)changeHeadImg:(UIImage *)headImg withURL:(NSString *)url
{
[self showHudInView:self.view hint:NSLocalizedString(@"上传中...", @"上传中...")];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//设置相应内容类型
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/xml"];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//UIImage *image = [UIImage imageNamed:@"test.jpg"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//将图片类型压缩后转成NSData对象
NSData *imgData = UIImageJPEGRepresentation(headImg, 0.1);
//获取当前的上传的时间 作为名字
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
//这个可以带参数传也可以不带参数
//NSDictionary *parameters = @{@"userNam":name};
[manager POST:url parameters:nil constructingBodyWithBlock:^(id formData) {
//图片参数@"headImg" 图片名称 fileName 图片的内容 imgData 图片的类型 @"image/png"
[formData appendPartWithFileData:imgData name:@"headImg" fileName:fileName mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self hideHud];
customView.backImg.image = headImg;
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
post 上传多张图片
-(void)Senderdynamic:(SenderDynamicModel *)senderModel winthURl:(NSString *)url
{
[self showHudInView:self.view hint:NSLocalizedString(@"发表中", @"发表中...")];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/xml"];//设置相应内容类型
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"tag":senderModel.tag,@"content":senderModel.content};
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id formData) {
//这里用for循环吧要上传的图片拼接(怎么上传多张图片跟服务期有很大的关系不一定试用)
for (int i =0; i < allArrayImg.count; i ++)
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSData *imgData = UIImageJPEGRepresentation([allArrayImg objectAtIndex:i],0.1);
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
[formData appendPartWithFileData:imgData name:@"img" fileName:fileName mimeType:@"image/png"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
TTAlertNoTitle(NSLocalizedString(@"发表成功", @"发表成功"));
[self hideHud];
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self];
}];
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
TTAlertNoTitle(NSLocalizedString(@"发表失败", @"发表失败"));
[self hideHud];
NSLog(@"Error: %@", error);
}];
}