今天看了下AFNetwoking这个通讯库,简单研究了一下
1。发一个get请求,
AFHTTPClient *bClient = [AFHTTPClient clientWithBaseURL
[NSURLURLWithString:@"http://xxxx.com"]];
[bClientgetPath:nil parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject)
{
NSLog(@"response description:%@",[responseObjectdescription]);
}
failure:^(AFHTTPRequestOperation *operation,NSError *error)
{
NSLog(@"error description:%@",[error description]);
}];
2。发一个post请求
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"123",@"num",@"jack",@"name",nil];
AFHTTPClient *aClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.xxxx.com"]];
//设置接受的数据类型为json
[aClientsetDefaultHeader:@"Accept"value:@"application/json"];
//设置提交的数据编码类型为json格式
[aClient setParameterEncoding:AFJSONParameterEncoding];
[aClientpostPath:nil parameters:dic success:^(AFHTTPRequestOperation*operation,id responseObject)
{
NSLog(@"response description:%@",[responseObject description]);
}
failure:^(AFHTTPRequestOperation *operation,NSError *error)
{
NSLog(@"error description:%@",[errordescription]);
}];