目錄
<h2 id="1"> 網(wǎng)絡(luò)請求 </h2>
- <h4 id="1.1">POST方法 </h4>
需要借助AFNetworking
NSString *url = @"http://xxxxxxxxxxxxxxxxxxxx";
NSDictionary *param = @{@"key":@"value"};
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"成功%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失敗%@", error);
}];
- <h4 id="1.2">GET方法 </h4>
需要借助AFNetworking
NSString *url = @"http://xxxxxxxxxxxxxxxxxxxx; //網(wǎng)址
NSDictionary *param = @{@"key":@"value"};//參數(shù)
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
NSLog(@"KEY :%@",[json valueForKey:@"KEY"]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"錯誤:%@",error);
}];