最近在做IOS的开发,网络请求采用了AFNetworking框架,由于服务器端是接收JSON的数据,新框架不太熟,网上搜了好久才解决,记录一下。
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"apple" forKey:@"brand"];NSString *url=@"http://xxxxx";//你的接口地址 AFHTTPRequestOperationManager *manager = [A FHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//如果报接受类型不一致请替换一致text/html或别的
manager.requestSerializer=[AFJSONRequestSerializer serializer];//申明请求的数据是json类型
[manager POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
}
];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"apple" forKey:@"brand"];NSString *url=@"http://xxxxx";//你的接口地址 AFHTTPRequestOperationManager *manager = [A FHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//如果报接受类型不一致请替换一致text/html或别的
manager.requestSerializer=[AFJSONRequestSerializer serializer];//申明请求的数据是json类型
[manager POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
}
];