Ios_ Network Request _ Proxy mode

Source: Internet
Author: User

#pragma mark - 网络请求代理方式(异步)- (IBAction)DelegateButtonDidClicked:(UIButton *)sender { // 1、拼接 urlString,网址里面必须写 http:// NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?

date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; // 2、依据字符串创建 URL(统一资源定位符) NSURL *url = [NSURL URLWithString:urlString]; // 3、依据 url 创建 request 请求类的对象 NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 4、请求数据(代理回调方式)遵守代理 <NSURLConnectionDataDelegate> [NSURLConnection connectionWithRequest:request delegate:self];}

Proxy method
Client receives response from server

#pragma mark | client收到server的响应- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    // 仅仅运行一次    // 初始化 receiveData 对象    self.receiveData = [NSMutableData data];}

Client receiving data (not all at once)

#pragma| client收数据(并不是一次性所有拿到)- (void**)data{    // 每次返回的都是新数据    // 拼接数据(请求一点 拼接一点)    [self.receiveData appendData:data];}

Client receives data completion

#pragma mark | Client receives data completion- (void) Connectiondidfinishloading: (nsurlconnection*) connection{//Parse Data    nsdictionary*dict = [Nsjsonserialization jsonobjectwithdata: Self. ReceivedataOptions: (nsjsonreadingmutablecontainers) Error:Nil];Nsarray*array = dict[@"News"]; Self. Newsarray= [NsmutablearrayArray]; for(nsdictionary*smalldict in array) {Newsmodal *modal = [[Newsmodal alloc] init];        [Modal setvaluesforkeyswithdictionary:smalldict]; [ Self. NewsarrayAddobject:modal]; } for(Newsmodal *modal in Self. Newsarray) {NSLog(@"%@", modal. Title); }}

Network request failed

#pragma mark | 网络请求失败- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"%@", error);}

Ios_ Network Request _ Proxy mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.