網絡解析

網絡解析 又叫網絡編程、網絡請求
完整的網絡請求分爲三步:
1>url NSURL 請求網絡地址
2>request 請求對象
3>connection 連接對象
其中2>又分爲兩種:get請求和post請求
3>也分爲兩種:同步連接和異步連接
另外 同步連接分爲兩種get和post,分別對應2>中的get和post
異步連接也分爲兩種:delegate 和 block
delegate的設置又分爲兩種 alloc init 方法 和 便利構造器方法
注意:如果網絡地址格式爲:http://xxx?strDate=2015-08-08&strRow=1&strMS=1 則2>中get請求和post請求都可用,
如果網絡地址格式爲:http://xxx.php 則2>中只能用get


具體方法如下:
方法一、get請求 同步連接
1>請求網絡地址
NSURL *url = [NSURL URLWithString:@“http://xxxx /getC_N?strDate=2015-08-09&strRow=1&strMS=1”];
2>請求對象
NSURLRequest *request = [[NSURLRequest alloc] initWithURL : url cachePolicy : 0 timeoutInterval : 10];//裏面填的三項分別爲:url 緩衝策略 最長請求時間
3>建立連接
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:get_request returningResponse:&response error:&error];
//解析網絡數據
NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *contentEntityDic = [rootDic objectForKey:@“contentEntity”];
NSString *sGW = contentEntityDic[@“sGW”];
NSLog(@"%@",sGW);


方法二、post請求 同步連接
1>請求網絡地址
NSURL *url2 = [NSURL URLWithString:@“http://xxxx”];
2>請求對象
NSMutableURLRequest *post_request = [[NSMutableURLRequest alloc]initWithURL:url2 cachePolicy:0 timeoutInterval:10];
[post_request setHTTPMethod:@“POST”];
[post_request setHTTPBody:[@“strDate=2015-08-09&strRow=1&strMS=1” dataUsingEncoding:NSUTF8StringEncoding]];
3>建立連接 post方法(保密性較高)
NSData *data2 = [NSURLConnection sendSynchronousRequest : request returningResponse:nil error:nil];
//解析網絡數據
NSDictionary *rootDic2 = [NSJSONSerialization JSONObjectWithData:data2 options:0 error:nil];
NSDictionary *contentDic = [rootDic2 objectForKey:@“contentEntity”];
NSString *sGW2 = contentDic[@“sGW”];
NSLog(@"%@",sGW2);


方法三、get 請求 異步連接block方法
1>、2>同 get請求 同步連接get方法
3>
[NSURLConnection sendAsynchronousRequest : request queue : [NSOperationQueue mainQueue] completionHandler : ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@",response);
//解析網絡數據data(放在內部解析)
NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];//注意 data是請求完成之後的data
NSDictionary contentEntityDic = [rootDic objectForKey:@“contentEntity”];
NSString sGW = contentEntityDic[@“sGW”];
NSLog(@"%@",sGW);
}];
方法四、get 請求 異步連接delegate方法
1>、2>同 get請求 同步連接get方法
3>
方法1.alloc init設置 delegate
[[[NSURLConnection alloc]initWithRequest:get_request delegate:self]autorelease];
方法2.便利構器設置 設置delegate
[NSURLConnection connectionWithRequest:get_request delegate:self];
/

第二種異步鏈接請求方法 :
代理的優點:追蹤請求過程
給NSURLConnection對象設置代理 有兩種那個方法,一般使用第二種方法
*/
detegate 的解析網絡數據放在 viewDidLoad外面

  • (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    //1.
    NSLog(@"%s%d%@",FUNCTION,LINE,@“收到網絡迴應”);
    //2.初始化可變數據容器
    self.receiveData = [NSMutableData data];
    }
    // 接收數據的時候出發,參數data爲本次接收到的數據
  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    //拼接data
    [self.receiveData appendData:data];
    NSLog(@"%s%d%@",FUNCTION,LINE,@“正在打印數據”);
    }
    //數據接收完成之後執行下面的方法
  • (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    //此時的data爲 接收完成之後的data,可以解析了
    NSLog(@"%s%d%@",FUNCTION,LINE,@“數據傳輸完成”);
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:self.receiveData options:0 error:nil];
    NSDictionary *contentDic = [rootDic objectForKey:@“contentEntity”];
    NSString *sGW = contentDic[@“sGW”];
    NSLog(@"%@",sGW);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章