//post提交json数据
+ (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters success:(void (^)(NSDictionary *respinseDic, id responseObject))success fail:(void (^)())fail
{
//通过单例实例化对象
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/json",@"text/plain",@"text/html",nil];
// 设置超时时间
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 8.f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
//第二个参数需要传入字典
[manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
NSString *html = operation.responseString;
NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"dict %@",dict);
NSLog(@"%@",[dict objectForKey:@"msg"]);
success(dict,responseObject);
}
// NSLog(@"%@",responseObject);
// NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// NSLog(@"str = %@",str);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
if (fail) {
fail();
}
[Public showToast:Load_String(@"internerWrong")];
}];}
这个是我项目中的一段