本文主要是介绍AFNetworking get请求与 post 请求操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
get 请求方式
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",kBYBaseURL]];NSURLRequest *request = [NSURLRequest requestWithURL:url];AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {[self showFooterView];// 刷新表格[mTableView reloadData];// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态[_footer endRefreshing];//提交结果信息到服务器} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态[_footer endRefreshing];}];[jsonOperation start];
post 请求方式
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];//这里要将url设置为空httpClient.parameterEncoding = AFJSONParameterEncoding;[httpClient setDefaultHeader:@"Accept" value:@"text/json"];[httpClient setDefaultHeader:@"appKey" value:str_AppKey];[httpClient setDefaultHeader:@"sdkVersion" value:app_Version];// 要传递的json数据是一个字典NSDictionary *params=[[NSDictionary alloc] init];
// 要传递的json数据是一个字典params =@{@"ad":@{@"adType":@4,@"category":@1,@"lastAdId":@0},@"device":@{@"appKey":@"d645920e395fedad7bbbed0eca3fe2e0",@"appList":@[@{@"packageName":@"cn.domob.offer.wall.android"}, @{@"packageName":@"com.alipay.android.app"},@{@"packageName":@"com.android.demo.charge"},@{@"packageName":@"com.example.customtest"},@{@"packageName":@"com.example.networktest"},@{@"packageName":@"com.floppy.lion.jump"},@{@"packageName":@"com.qihoo.appstore"},@{@"packageName":@"com.qihoo.permmgr"},@{@"packageName":@"com.qihoo360.mobilesafe"},@{@"packageName":@"com.tencent.android.qqdownloader"},@{@"packageName":@"com.tencent.mm"},@{@"packageName":@"com.tencent.mm.openapi"},@{@"packageName":@"com.tencent.qqpimsecure"},@{@"packageName":@"com.unionpay.uppay"},@{@"packageName":@"com.wall.sample"},@{@"packageName":@"com.youmi.android.sample"},@{@"packageName":@"com.yskj.app.wnwapn"},@{@"packageName":@"org.ad.creditswall"}],@"brand":@"samsung",@"devAppPackage":@"org.ad.creditswall",@"imei":@"357558052916935",@"mobileSystem":@"android",@"model":@"GT-S7568",@"network":@"wifi",@"operator":@"01",@"resolution":@"480*800",@"sdkVersion":@"1.0",@"systemVersion":@"4.0.4"}};// httpClient 的postPath就是上文中的pathStr,即你要访问的URL地址,这里是向服务器提交一个数据请求,[httpClient postPath:kBYBaseURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {[self showFooterView];NSLog(@"params %@",params);NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];NSLog(@"Request Successful, response '%@'", responseStr);// 刷新表格[mTableView reloadData];// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态[_header endRefreshing];//提交结果信息到服务器} failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"[HTTPClient Error]: %@", error);[_header endRefreshing];}];
这篇关于AFNetworking get请求与 post 请求操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!