iphone网络post连接的两种处理方式(同步和异步)(zz)

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 10:38 点击:

第一种: 直接返回方式。
-(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{

NSLog(urlstr);
NSLog(strcontext);
assert(strcontext != NULL);
assert(urlstr != NULL);

NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlstr]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSURLResponse *respone;
NSError *error;
NSData *myReturn =[NSURLConnection sendSynchronousRequest:request returningResponse:&respone
error:error];
NSLog(@"%@", [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding]);
}

第二种,采用事件代理方式


-(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{

NSLog(urlstr);
NSLog(strcontext);
assert(strcontext != NULL);
assert(urlstr != NULL);

NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlstr]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
if (conn)   

NSLog(@"Connection success");
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[conn retain];

}   
else   

// inform the user that the download could not be made 

}

 

// 收到响应时, 会触发
- (void)connection:(NSURLConnection *)connection  didReceiveResponse:(NSURLResponse *)response  {
// 注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary description]);
NSLog(@"%d",[response statusCode]);

}
}


// Forward errors to the delegate.
//链接错误  
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
NSLog(@"%@",[error localizedDescription]);

}

// Called when a chunk of data has been downloaded.
//接收数据 每收到一次数据, 会调用一次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Process the downloaded chunk of data.

NSLog(@"%d", [data length]);
//NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

//[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];


}
//接收结束
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSLog(@"%@",connection);
//NSLog(@"%lld", received_);

//[self

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Lexical or Preprocessor Issue 'xxx.h
      • ios学习笔记(二)xcode 4.3.2下实现基本交互
      • ios版本的helloworld
      • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
      • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
      • IOS类似iphone通讯录TableView的完整demo【附源码】
      • UITableView一些方法
      • [iPhone中级]iPhone团购信息客户端的开发 (二)
      • iphone(object-c)内存管理(1)
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1