概述
这篇文章主要说一下AFNetWorking的get与post用法以及使用AFNetWorking进行图片上传的问题
首先,是AFNetWorking的get用法:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// 设置超时时间
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 8.0f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
// 不让AFNetworking自动解析下载下来的内容
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
DLog(@"success json = %@",operation.responseString);
id result = nil;
result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
if (result == nil) {
}
else
{
//一些自己要的操作
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
DLog(@"error = %@",error);
// failure(error);
}
}];
2.AFNetWorking的post用法
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// 设置超时时间
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 8.0f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
// 不让AFNetworking自动解析下载下来的内容
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//body为要传给服务器的参数(字典)
[manager POST:urlStr parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success){
DLog(@"success json = %@",operation.responseString);
id result = nil;
result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
if (result == nil) {
}
else{
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
DLog(@"error = %@",error);
}
}];
UIImage *image = imgArr[0];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 10.0f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
// 不让AFNetworking自动解析下载下来的内容
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:urlStr parameters:body constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[formData appendPartWithFileData:imageData name:@"file" fileName:@"headIcon.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success){
DLog(@"success json = %@",operation.responseString);
id result = nil;
result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
if (result == nil) {
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
DLog(@"error = %@",error);
}
}];
最后
以上就是安详眼睛为你收集整理的AFNetWorking的get与post用法以及进行图片上传的全部内容,希望文章能够帮你解决AFNetWorking的get与post用法以及进行图片上传所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复