关于ios开发。常见的网络请求有GET请求, POS请求,
给服务器传输数据的方式不同:
1.GET是通过网址字符串,所传输的数据显示在网址里。
2.POST是通过data,数据被转换,无法直接读取,。
数据链接方式:
-同步连接:只有数据请求完成了之后才会执行后面的代码,只做一件事,容易出现页面的卡死。
-异步连接:在请求数据的同时不影响后面代码的执行。可以同时去做多件事,
get请求:
//创建URL对象
NSString *urlStr = [kAuthorizeURLstringByAppendingFormat:@"?display=mobile&client_id=%@&redirect_uri=%@",kAppKey, kRedirectURI];
NSURL *url = [NSURLURLWithString:urlStr];
//创建请求(封装一个请求,保存发给服务器的全部数据)
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
//发送请求
[NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *_Nullable response, NSData * _Nullable data,NSError * _Nullable connectionError) {
if (!connectionError) {
}else{
}
}];
// 1.创建post请求路径
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:kBaseURL]];
NSURLRequest *post = [client requestWithMethod:method path:path parameters:@{ }];
// 2.创建AFJSONRequestOperation对象
NSOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:post
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
if (success == nil) return;
success(JSON);
}
failure : ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
if (failure == nil) return;
failure(error);
}];
// 3.发送请求
[op start];
}