ios同步与异步网络请求

来源:互联网 发布:网络优化工程师 怎么样 编辑:程序博客网 时间:2024/04/27 22:14
  1. 1、同步请求

    Object-c代码  收藏代码
    1. - (void) initRequestData : (RequestVO *) requestParam{  
    2.     NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];  
    3.     NSMutableDictionary *directory = [[NSMutableDictionary alloc]init];  
    4.     NSString *paramJson;  
    5.     NSMutableString *urlStr = [[NSMutableString alloc] initWithString:REQUEST_URL];  
    6.     [directory setValue:requestParam.className forKey:@"className"];  
    7.     [directory setValue:requestParam.methodName forKey:@"methodName"];  
    8.     [paramDic setValue:requestParam.specialCode forKey:@"specialCode"];  
    9.     [paramDic setValue:requestParam.start forKey:@"start"];  
    10.     [paramDic setValue:requestParam.end forKey:@"end"];  
    11.     [directory setValue:paramDic forKey:@"parameter"];  
    12.     if ([NSJSONSerialization isValidJSONObject:directory]) {  
    13.         NSError *error ;  
    14.         NSData *jsonData = [NSJSONSerialization dataWithJSONObject:directory options:NSJSONWritingPrettyPrinted error:&error];  
    15.         paramJson =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  
    16.         paramJson = [paramJson stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
    17.     }  
    18.     [urlStr appendString:paramJson];  
    19.     NSURL *url = [NSURL URLWithString:urlStr];  
    20.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];  
    21.     [request setURL:url];  
    22.     [request setHTTPMethod:@"POST"];  
    23.     NSHTTPURLResponse *response;  
    24.     NSError *error ;  
    25.     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];  
    26.       
    27.     NSDictionary *dataDictory = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];  
    28.     NSLog(@"dataDictory : %@",[dataDictory description]);  
    29.     }  
    30. }  

     2、异步请求

    Object-c代码  收藏代码
    1. @interface CatalogViewController : UIViewController<NSURLConnectionDataDelegate>  
    2.   
    3. @property (strong,nonatomic) NSMutableData *receivedData;  
    4. @end  
    5.   
    6.   
    7. //  
    8. //  CatalogViewController.m  
    9. //  IpadLisShow  
    10. //  
    11. //  Created by Dwen on 13-1-21.  
    12. //  Copyright (c) 2013年 Dwen. All rights reserved.  
    13. //  
    14.   
    15. #import "CatalogViewController.h"  
    16. #import <QuartzCore/QuartzCore.h>  
    17. @interface CatalogViewController ()  
    18.   
    19. @end  
    20.   
    21. @implementation CatalogViewController  
    22. @synthesize receivedData;  
    23.   
    24. //初始化请求数据  
    25. - (void) initRequestData : (RequestVO *) requestParam{  
    26.     NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];  
    27.     NSMutableDictionary *directory = [[NSMutableDictionary alloc]init];  
    28.     NSString *paramJson;  
    29.     NSMutableString *urlStr = [[NSMutableString alloc] initWithString:REQUEST_URL];  
    30.     [directory setValue:requestParam.className forKey:@"className"];  
    31.     [directory setValue:requestParam.methodName forKey:@"methodName"];  
    32.     [paramDic setValue:requestParam.specialCode forKey:@"specialCode"];  
    33.     [paramDic setValue:requestParam.orderPa forKey:@"orderPa"];  
    34.     [paramDic setValue:requestParam.sort forKey:@"sort"];  
    35.     [paramDic setValue:requestParam.start forKey:@"start"];  
    36.     [paramDic setValue:requestParam.end forKey:@"end"];  
    37.     [directory setValue:paramDic forKey:@"parameter"];  
    38.     NSLog(@"[directory description] :%@",[directory description]);  
    39.     if ([NSJSONSerialization isValidJSONObject:directory]) {  
    40.         NSError *error ;  
    41.         NSData *jsonData = [NSJSONSerialization dataWithJSONObject:directory options:NSJSONWritingPrettyPrinted error:&error];  
    42.         paramJson =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  
    43.         paramJson = [paramJson stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
    44.     }  
    45.     [urlStr appendString:paramJson];  
    46.     NSURL *url = [NSURL URLWithString:urlStr];  
    47.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];  
    48.     [request setURL:url];  
    49.     [request setHTTPMethod:@"POST"];  
    50.     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];  
    51.     if (connection) {  
    52.         receivedData = [[NSMutableData alloc] init];  
    53.     }else{  
    54.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器连接异常" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
    55.         [alert show];  
    56.     }  
    57. }  
    58.   
    59. #pragma connection  
    60. //接收响应  
    61. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{  
    62.     [receivedData setLength:0];  
    63. }  
    64.   
    65. //接收到数据  
    66. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{  
    67.     [receivedData appendData:data];  
    68. }  
    69.   
    70. //数据加载完成  
    71. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{  
    72.     NSError *error ;  
    73.     NSDictionary *dataDictory = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];  
    74.     //    NSLog(@"dataDictory : %@",[dataDictory description]);  
    75.     if (NULL != dataDictory) {  
    76.         //拍品信息  
    77.           
    78.     }else{  
    79.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该拍品无数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
    80.         [alert show];  
    81.     }  
    82. }  
    83.   
    84. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{  
    85.     NSLog(@"Error : %@",error);  
    86. }  
    87.   
    88. @end  

0 0
原创粉丝点击