本文介紹的是AFNetworking-2.0 使用方法(增長適應:不完善的head內的 meta的content格式)json
隨着asihttprequest的中止更新,許多人都轉向了AFNetworking、 MKNetworkKit.我也是其中一個。因而我從網上找了許多文章做參考,可是結果都是失敗了結。研究了很久都搞不透,最後仍是請人幫忙搞定了。常常從網上索取免費資料的一員,要有回報的思想,也爲了讓更多的人少走些彎路,因此下面是代碼:(有錯誤能夠指出)app
首先:將AFNetworking、UIKit+AFNetworking 加入到工程post
通常要引用這兩個包,否則會有錯誤提示:SystemConfiguration.framework、 MobileCoreServices.frameworkurl
而後在要使用的地方spa
#import "AFHTTPRequestOperationManager.h".net
#import "AFHTTPSessionManager.h"orm
AFHTTPRequestOperationManager的post有兩個方法,一個是普通的post,另外一個是能夠上傳圖片的htm
1.上傳圖片:blog
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
NSDictionary *parameters =@{@"參數1":@"value1",@"參數2":@"value2"、、、};
[managerPOST:@"替換成你要訪問的地址"parameters:parameters
success:^(AFHTTPRequestOperation *operation,id responseObject) {
NSLog(@"Success: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"Error: %@", error);
}];
tom19830924 | 2013-12-27 17:44 |
這是我第二次回答這問題了 這不是AFNetworking的問題 這是作Server那邊的人沒把head內的 meta的content格式指定好 請參考 http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type |
evangel | 2014-03-18 21:41 |
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; |
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
manager.requestSerializer = [AFJSONRequestSerializerserializer];
manager.responseSerializer = [AFJSONResponseSerializerserializer];
[manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Accept"];
[manager.requestSerializersetValue:@"application/json; charset=utf-8"forHTTPHeaderField:@"Content-Type"];