zoukankan      html  css  js  c++  java
  • iphone网络post连接的两种处理方式(同步和异步)

    iphone网络post连接的两种处理方式(同步和异步)  
    第一种: 直接返回方式。
    -(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];  
    
    NSURLResp*****e *respone;
    NSError *error;
    NSData *myReturn =[NSURLConnection sendSynchronousRequest:request returningResp*****e:&respone
    error:error];
    NSLog(@"%@", [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding]);
    }
    第二种,采用事件代理方式(重要)
    使用TouchXML时,常用到下面的代码
    
    -(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  didReceiveResp*****e:(NSURLResp*****e *)resp*****e  {
    // 注意这里将NSURLResp*****e对象转换成NSHTTPURLResp*****e对象才能去
    NSHTTPURLResp*****e *httpResp*****e = (NSHTTPURLResp*****e*)resp*****e;
    if ([resp*****e respondsToSelector:@selector(allHeaderFields)]) {
    NSDictionary *dictionary = [httpResp*****e allHeaderFields];
    NSLog([dictionary description]);
    NSLog(@"%d",[resp*****e 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 performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
    // Set the condition which ends the run loop.
    }
  • 相关阅读:
    java基础面试题:说说&和&&的区别
    java基础面试题:java中有没有goto? 在JAVA中如何跳出当前的多重嵌套循环?
    Java基础面试操作题: 获取 1-20 之间的随机数,共计 20 个,要求不能重复 获取 1-20 之间的随机数,共计 10 个,要求不能重
    Intellij设置Eclipse 快捷键
    Intellij 安装sonarlint
    Intellij 关闭参数提示
    Linux中Firefox——Firebug插件安装及使用
    Linux中Firefox——Httpfox插件安装及使用
    OpenCV——Haar-like特征
    Python学习笔记6(列表生成式)
  • 原文地址:https://www.cnblogs.com/gaoxiao228/p/2582351.html
Copyright © 2011-2022 走看看