//
// 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