AFJSONRequestSerializer

AFJSONRequestSerializer


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

你可能感兴趣的:(iOS)