ASIHttpRequst 之異步請求

//

//  RootViewController.m

//  ASIHttpRequest

//

//  Created by Hallin_Me on 14-10-7.

//  Copyright (c) 2014 hallin_me. All rights reserved.

//

/*

  使用方法:

  ASIHttpRequst第三方類庫 不支持arc 

  使用前導入下面4個系統包

   libz.1.1.3.dylib,MobileCoreServices.framework,SystemConfiguration.framework,CFNetwork.framework

*/

#define URL_PATH @"http://sp.autohome.com.cn/clubapp/jingxuan/HomeJingHua.ashx?pageSize=20&pageIndex=0"

#import "RootViewController.h"

#import "ASIHTTPRequest.h"

@interface RootViewController ()


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //ASI請求

    [self createASIHttp];

    self.view.backgroundColor=[UIColor redColor];

}

-(void)createASIHttp{

    //封裝URL

    NSURL *url=[NSURL URLWithString:URL_PATH];

    //創建ASIHttp請求對象

    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];

    //協議

    request.delegate=self;

    //開始請求,開啓異步請求

    [request startSynchronous];

}


#pragma mark ASIHTTPRequestDelegate

-(void)requestStarted:(ASIHTTPRequest *)request{

    NSLog(@"開始請求!");

}

-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{

    NSLog(@"建立聯繫!");

}

-(void)requestFinished:(ASIHTTPRequest *)request{

//    NSString *str=[[NSString alloc]initWithData:request.responseData encoding:NSUTF8StringEncoding];

//    NSLog(@"%@",str);

    //如若上面NSUTF8StringEncoding格式轉換沒用,鏈接的數據使用的GBK格式,所以data數據要格式轉換

    NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

    NSString *str=[[NSString alloc]initWithData:request.responseData encoding:gbkEncoding];

    NSLog(@"%@",str);

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章