ios NSURLRequest NSMutableURLRequest 数据请求
来源:互联网 发布:mac版金蝶友商智慧记 编辑:程序博客网 时间:2024/06/06 01:17
转自:http://www.maxiaoguo.com/clothes/256.html
get 请求
- #pragma mark - GET登录
- - (void)getLogon
- {
-
- NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", self.userName.text, self.userPwd.text];
-
- NSURL *url = [NSURL URLWithString:urlStr];
-
-
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
-
-
-
-
-
- [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
-
- if (connectionError == nil) {
-
-
- NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
-
-
- NSLog(@"%@ %@", str, [NSThread currentThread]);
-
-
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- self.logonResult.text = @"登录完成";
- }];
- }
- }];
-
-
- NSLog(@"come here %@", [NSThread currentThread]);
-
- NSURLResponse *response = nil;
-
-
-
-
-
-
-
- [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
- }
post请求- #pragma mark - POST登录
- - (void)postLogon
- {
-
- NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"];
-
-
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
-
-
- request.HTTPMethod = @"POST";
-
- NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.userPwd.text];
-
- request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
-
-
- [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
-
- if (connectionError == nil) {
-
-
- NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
-
-
- NSLog(@"%@ %@", str, [NSThread currentThread]);
-
-
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- self.logonResult.text = str;
- }];
- }
- }];
-
-
- NSLog(@"come here %@", [NSThread currentThread]);
- }
使用NSURLConnection有两种方式: 第一种 如上, 第二种实现 NSURLConnectionDataDelegate 代理- - (void)getLogon
- {
-
- NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", self.userName.text, self.myPwd];
-
- NSLog(@"%@", self.myPwd);
-
- NSURL *url = [NSURL URLWithString:urlStr];
-
-
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
-
-
- NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
-
-
- dispatch_async(dispatch_queue_create("demo", DISPATCH_QUEUE_CONCURRENT), ^{
- [connection start];
- });
- }
-
- #pragma mark - NSURLConnectionDataDelegate代理方法
- #pragma mark 接受到响应
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- {
-
-
- if (!self.data) {
- self.data = [NSMutableData data];
- } else {
- [self.data setData:nil];
- }
- }
-
- #pragma mark 接收到数据,如果数据量大,例如视频,会被多次调用
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
-
- [self.data appendData:data];
- }
-
- #pragma mark 接收完成,做最终的处理工作
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
-
- NSString *str = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
-
- NSLog(@"%@ %@", str, [NSThread currentThread]);
- }
-
- #pragma mark 出错处理,网络的出错可能性非常高
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- {
- NSLog(@"%@", error.localizedDescription);
- }
注: 更新UI都要在主线程更新,原因要保证线程安全-
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- self.logonResult.text = str;
- }];
0 0