IOS学习笔记——网络请求

1.

#pragma mark -使用AFNetworking方式请求数据
-(void) requestUseAFNetWorking{
    NSString *postUrl =[[self generateUrlStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//
    NSLog(@"postUrl: %@", postUrl);
//    [[AFAppDotNetAPIClient sharedClient].requestSerializer ];
    [[AFAppDotNetAPIClient sharedClient] POST:postUrl parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        isLoading = NO;
        //    if (!noRefresh) {
        //        [self clear];
        //    }
        NSLog(@"JSON: %@", responseObject);
        @try {
            NSDictionary *jsonData1 = [responseObject objectFromJSONData];
            NSLog(@"jsonData1 = %@", jsonData1);
            
//            NSArray *arr = [responseObject ];
            //    NSDictionary *projectDic = [request.responseData objectFromJSONData];
            //    //    NSLog(@"projectDic = %@", projectDic);
            //    NSArray *arr = [projectDic objectForKey:@"projects"];
            //    //    NSLog(@"arr = %@", arr);
            //
            //    for (NSDictionary *dic in arr) {
            //        ZZNProject *n = [[ZZNProject alloc] initProjectWithDic:dic andState:PS_NA];
            //        
            //        [projectArray addObject:n];
            //    }
            
//                                                   NSMutableArray *newNews = self.catalog <= 1 ?
        //                                           [Tool readStrNewsArray:operation.responseString andOld: news]:
        //                                           [Tool readStrUserBlogsArray:operation.responseString andOld: news];
        //
        //                                           int count = [Tool isListOver2:operation.responseString];
        //                                           allCount += count;
        //                                           if (count < 20)
        //                                           {
        //                                               isLoadOver = YES;
        //                                           }
        //                                           [news addObjectsFromArray:newNews];
        //                                           [self.tableNews reloadData];
        //                                           [self doneLoadingTableViewData];
        //
        //                                           //如果是第一页 则缓存下来
        //                                           if (news.count <= 20) {
        //                                               [Tool saveCache:5 andID:self.catalog andString:operation.responseString];
        //                                           }
        }
        @catch (NSException *exception) {
//  [NdUncaughtExceptionHandler TakeException:exception];
        }
        @finally {
            [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:0.0];
//            [self doneLoadingTableViewData];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error: %@", error);
        
        isLoading = NO;
        
//        [self doneLoadingTableViewData];
        [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:0.0];
    }];

}

2.

#pragma mark - 使用ASIHTTPRequest,block请求方式
- (void) requestUseASIHTTP2{
    __block ASIHTTPRequest *blockRequest = [ASIHTTPRequest requestWithURL:[self generateURL]];
    blockRequest.requestMethod = @"POST";
    [blockRequest setCompletionBlock:^{
        NSString *responseString = [blockRequest responseString];
        NSLog(@"请求的String数据:%@",responseString);
        
    }];
    [blockRequest setFailedBlock:^{
        NSError *error = [blockRequest error];
        NSLog(@"Error:%@",error.userInfo);
    }];
    [blockRequest startAsynchronous];//发送异步请求
}

3.


你可能感兴趣的:(IOS学习笔记——网络请求)