Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5542421
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: iOS平台

2014-02-24 16:17:10


点击(此处)折叠或打开

  1. // 判断服务器后台广告开关是否打开
  2. // 服务器返回的数据格式如下: 
  3. [{"pid":"120005","id":0,"is_open":"0"}]

  4. -(BOOL)AdIsOpen
  5. {
  6.     BOOL retIsOpen = false;
  7.     
  8.     NSString* postUrl=@"";
  9.     NSString* strPid =@"110005";
  10.     NSString* strDownPid =@"120005"; // 下传数据的 pid
  11.     NSString* strId =@"1"; // 服务器后台分配的配置 id
  12.     
  13.     
  14.     NSMutableDictionary *postDict = [[NSMutableDictionary alloc] init];
  15.     
  16.     // 设置 post 提交的数据
  17.     [postDict setObject:@"110005" forKey:@"pid"];
  18.     [postDict setObject:@"1" forKey:@"id"];
  19.     
  20.     
  21.     NSString *jsonStr = [self sendRequestTo:[NSURL URLWithString:postUrl] usingVerb:@"POST" withParameters:postDict];
  22.     NSLog(@" verify_server repost= %@", jsonStr);
  23.     
  24.     NSDictionary *retDict = [jsonStr JSONValue];
  25.     //NSLog(@"do post: retDict = %@, %@", retDict, [retDict description]);
  26.     
  27.     //得到词典中所有Value值
  28.     NSEnumerator * enumeratorValue = [retDict objectEnumerator];
  29.     
  30.     //快速枚举遍历所有Value的值
  31.     for (NSObject *object in enumeratorValue)
  32.     {
  33.         //NSLog(@"遍历Value的值: %@",object);
  34.         
  35.         if([object isKindOfClass:[NSDictionary class]])
  36.         {
  37.             NSDictionary* dict = (NSDictionary*)object;
  38.             
  39.             
  40.             // 当前的协议数据 pid = 120005
  41.             if( [[dict objectForKey:@"pid"] isEqualToString:strDownPid] && // 广告开关数据
  42.                  [[dict objectForKey:@"is_open"] intValue]==1)
  43.             {
  44.                 retIsOpen = true;
  45.                 NSLog(@"AdIsOpen :true");
  46.             }
  47.         }
  48.     }

  49.     [postDict release];
  50.     postDict = nil;
  51.     
  52.     return retIsOpen;
  53.     
  54. }


  55. /**
  56.  * url:请求地址
  57.  * verb:请求方式, 默认传递 @"POST" 就可以
  58.  * parameters:请求参数
  59.  */
  60. -(NSString *)sendRequestTo:(NSURL *)url usingVerb:(NSString *)verb withParameters:(NSDictionary *)parameters
  61. {
  62.     NSString *jsonStr = nil;
  63.     NSData *body = nil;
  64.     NSMutableString *params = nil;
  65.     NSString *contentType = @"text/html; charset=utf-8";
  66.     NSURL *finalURL = url;
  67.     if(nil != parameters){
  68.         params = [[NSMutableString alloc] init];
  69.         for(id key in parameters){
  70.             NSString *encodedkey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  71.             
  72.             CFStringRef value = (__bridge CFStringRef)[[parameters objectForKey:key] copy];
  73.             CFStringRef encodedValue = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, value,NULL,(CFStringRef)@";/?:@&=+$", kCFStringEncodingUTF8);
  74.             [params appendFormat:@"%@=%@&", encodedkey, encodedValue];
  75.             CFRelease(value);
  76.             CFRelease(encodedValue);
  77.         }
  78.         [params deleteCharactersInRange:NSMakeRange([params length] - 1, 1)];
  79.     }
  80.     //
  81.     if([verb isEqualToString:@"POST"]){
  82.         contentType = @"application/x-www-form-urlencoded; charset=utf-8";
  83.         body = [params dataUsingEncoding:NSUTF8StringEncoding];
  84.     }else{
  85.         if(nil != parameters){
  86.             NSString *urlWithParams = [[url absoluteString] stringByAppendingFormat:@"?%@", params];
  87.             finalURL = [NSURL URLWithString:urlWithParams];
  88.         }
  89.     }
  90.     NSMutableDictionary *headers = [[NSMutableDictionary alloc] init];
  91.     [headers setValue:contentType forKey:@"Content-Type"];
  92.     [headers setValue:@"text/html" forKey:@"Accept"];
  93.     [headers setValue:@"no-cache" forKey:@"Cache-Control"];
  94.     [headers setValue:@"no-cache" forKey:@"Pragma"];
  95.     [headers setValue:@"close" forKey:@"Connection"];
  96.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
  97.     [request setHTTPMethod:verb];
  98.     [request setAllHTTPHeaderFields:headers];
  99.     if(nil != parameters){
  100.         [request setHTTPBody:body];
  101.     }
  102.     params = nil;
  103.     //
  104.     NSURLResponse *response;
  105.     NSError *error = nil;
  106.     NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  107.     if(error){
  108.         NSLog(@"something is wrong: %@", [error description]);
  109.     }else{
  110.         if(responseData){
  111.             jsonStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  112.         }
  113.     }
  114.     return jsonStr;
  115. }

阅读(4642) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~