-
// 判断服务器后台广告开关是否打开
-
// 服务器返回的数据格式如下:
-
[{"pid":"120005","id":0,"is_open":"0"}]
-
-
-(BOOL)AdIsOpen
-
{
-
BOOL retIsOpen = false;
-
-
NSString* postUrl=@"";
-
NSString* strPid =@"110005";
-
NSString* strDownPid =@"120005"; // 下传数据的 pid
-
NSString* strId =@"1"; // 服务器后台分配的配置 id
-
-
-
NSMutableDictionary *postDict = [[NSMutableDictionary alloc] init];
-
-
// 设置 post 提交的数据
-
[postDict setObject:@"110005" forKey:@"pid"];
-
[postDict setObject:@"1" forKey:@"id"];
-
-
-
NSString *jsonStr = [self sendRequestTo:[NSURL URLWithString:postUrl] usingVerb:@"POST" withParameters:postDict];
-
NSLog(@" verify_server repost= %@", jsonStr);
-
-
NSDictionary *retDict = [jsonStr JSONValue];
-
//NSLog(@"do post: retDict = %@, %@", retDict, [retDict description]);
-
-
//得到词典中所有Value值
-
NSEnumerator * enumeratorValue = [retDict objectEnumerator];
-
-
//快速枚举遍历所有Value的值
-
for (NSObject *object in enumeratorValue)
-
{
-
//NSLog(@"遍历Value的值: %@",object);
-
-
if([object isKindOfClass:[NSDictionary class]])
-
{
-
NSDictionary* dict = (NSDictionary*)object;
-
-
-
// 当前的协议数据 pid = 120005
-
if( [[dict objectForKey:@"pid"] isEqualToString:strDownPid] && // 广告开关数据
-
[[dict objectForKey:@"is_open"] intValue]==1)
-
{
-
retIsOpen = true;
-
NSLog(@"AdIsOpen :true");
-
}
-
}
-
}
-
-
[postDict release];
-
postDict = nil;
-
-
return retIsOpen;
-
-
}
-
-
-
/**
-
* url:请求地址
-
* verb:请求方式, 默认传递 @"POST" 就可以
-
* parameters:请求参数
-
*/
-
-(NSString *)sendRequestTo:(NSURL *)url usingVerb:(NSString *)verb withParameters:(NSDictionary *)parameters
-
{
-
NSString *jsonStr = nil;
-
NSData *body = nil;
-
NSMutableString *params = nil;
-
NSString *contentType = @"text/html; charset=utf-8";
-
NSURL *finalURL = url;
-
if(nil != parameters){
-
params = [[NSMutableString alloc] init];
-
for(id key in parameters){
-
NSString *encodedkey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-
-
CFStringRef value = (__bridge CFStringRef)[[parameters objectForKey:key] copy];
-
CFStringRef encodedValue = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, value,NULL,(CFStringRef)@";/?:@&=+$", kCFStringEncodingUTF8);
-
[params appendFormat:@"%@=%@&", encodedkey, encodedValue];
-
CFRelease(value);
-
CFRelease(encodedValue);
-
}
-
[params deleteCharactersInRange:NSMakeRange([params length] - 1, 1)];
-
}
-
//
-
if([verb isEqualToString:@"POST"]){
-
contentType = @"application/x-www-form-urlencoded; charset=utf-8";
-
body = [params dataUsingEncoding:NSUTF8StringEncoding];
-
}else{
-
if(nil != parameters){
-
NSString *urlWithParams = [[url absoluteString] stringByAppendingFormat:@"?%@", params];
-
finalURL = [NSURL URLWithString:urlWithParams];
-
}
-
}
-
NSMutableDictionary *headers = [[NSMutableDictionary alloc] init];
-
[headers setValue:contentType forKey:@"Content-Type"];
-
[headers setValue:@"text/html" forKey:@"Accept"];
-
[headers setValue:@"no-cache" forKey:@"Cache-Control"];
-
[headers setValue:@"no-cache" forKey:@"Pragma"];
-
[headers setValue:@"close" forKey:@"Connection"];
-
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
-
[request setHTTPMethod:verb];
-
[request setAllHTTPHeaderFields:headers];
-
if(nil != parameters){
-
[request setHTTPBody:body];
-
}
-
params = nil;
-
//
-
NSURLResponse *response;
-
NSError *error = nil;
-
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
-
if(error){
-
NSLog(@"something is wrong: %@", [error description]);
-
}else{
-
if(responseData){
-
jsonStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
-
}
-
}
-
return jsonStr;
-
}
阅读(4642) | 评论(0) | 转发(1) |