码迷,mamicode.com
首页 > 移动开发 > 详细

iOS之数据请求NSURLConnection

时间:2015-06-05 17:33:39      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:nsurlconnection   数据请求   ios   

iOS之数据请求NSURLConnection


 NSString *lcsUrl = @"http://192.168.1.1:8080/lcsUrl";

    

    //如果网址中有汉字,需要先转码

    NSString *word = [NSString stringWithUTF8String:"汉字"];//注意此处到字符串要求传一个c的字符串,没有@

    

    NSString *finaUrl = [NSString stringWithFormat:@"http://192.168.1.1:8080/lcsUrl= %@",word];

    

    NSURL *url = [NSURL URLWithString:lcsUrl];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    request.HTTPMethod = @"GET";

    request.timeoutInterval = 60;

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

    [connection start];

请求出错

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"请求出错");

}


请求中接收到数据

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [_data appendData:data];

}

请求完毕

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSString *str = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];

    //在这里处理你收到的数据

}



iOS之数据请求NSURLConnection

标签:nsurlconnection   数据请求   ios   

原文地址:http://blog.csdn.net/lu_ca/article/details/46379137

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!