Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1071108
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-04-05 21:57:05

static CFRunLoopRef backgroundRunLoop;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

void
releaseContext(void* infoPtr)
{

CFRelease(infoPtr);
}
void* retainContext(void* infoPtr)
{

CFRetain(infoPtr);
return infoPtr;
}
void cancelTheStream(CFReadStreamRef readStream)
{
CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
CFReadStreamClose(readStream);
CFRelease(readStream);
}
void* threadMain(void* info)
{
pthread_mutex_lock(&mutex);
backgroundRunLoop=CFRunLoopGetCurrent();
CFRunLoopSourceContext context = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
CFRunLoopRun();
}
void createThread(void)
{
pthread_t threadID;
pthread_create(&threadID, NULL,threadMain, NULL);
}

void getResponse(CFReadStreamRef readStream,CFStreamEventType event,void* infoPtr)
{
if(event==kCFStreamEventEndEncountered){
// CFHTTPMessageRef response=CFHTTPMessageCreateEmpty(kCFAllocatorDefault, NO);
CFHTTPMessageRef response=(CFHTTPMessageRef)infoPtr;

UInt32 statusCode=CFHTTPMessageGetResponseStatusCode(response);
CFDataRef responseData=CFHTTPMessageCopyBody(response);

UInt32 count=CFDataGetLength(responseData);
CFStringRef responseJson=CFStringCreateWithBytes(kCFAllocatorDefault, CFDataGetBytePtr(responseData), count, kCFStringEncodingUTF8, YES);
CFRelease(responseJson);
CFRelease(responseData);
cancelTheStream(readStream);
return;
}
if(event==kCFStreamEventHasBytesAvailable){
UInt8 buffer[100];
int length=CFReadStreamRead(readStream, buffer,100);
CFHTTPMessageAppendBytes((CFHTTPMessageRef)infoPtr, buffer, length);
return;
}
if(event==kCFStreamEventErrorOccurred){
//CFStreamError error=CFReadStreamGetError(readStream);
cancelTheStream(readStream);
return;
}
return;
}

@implementation AppDelegate

@synthesize window = _window;
@synthesize versionKey;
@synthesize messageQueueKey;
- (void)dealloc
{
[_window release];
[super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//create the url
CFStringRef urlString=CFSTR("your url");
CFURLRef url=CFURLCreateWithString(kCFAllocatorDefault, urlString, NULL);

//create the request method
CFStringRef method=CFSTR("POST");

//create the http request
CFHTTPMessageRef request=CFHTTPMessageCreateRequest(kCFAllocatorDefault, method, url, kCFHTTPVersion1_1);

//set headers
CFStringRef headerName=CFSTR("Content-Type");
CFStringRef headerValue=CFSTR("application/json");
CFHTTPMessageSetHeaderFieldValue(request,headerName,headerValue);

//set body
CFStringRef jsonBody=CFSTR("your json data");
CFDataRef jsonData=CFStringCreateExternalRepresentation(kCFAllocatorDefault, jsonBody, kCFStringEncodingUTF8, 0);
CFHTTPMessageSetBody(request, jsonData);


CFHTTPMessageRef response=CFHTTPMessageCreateEmpty(kCFAllocatorDefault, NO);
CFStreamClientContext context={0,response,retainContext,releaseContext,NULL};

CFOptionFlags registeredEvent=kCFStreamEventHasBytesAvailable|kCFStreamEventEndEncountered|kCFStreamEventErrorOccurred;
CFReadStreamRef readStream=CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);

CFReadStreamSetClient(readStream, registeredEvent, getResponse, &context);
   if(backgroundRunLoop==NULL){
createThread();
pthread_cond_wait(&cond, &mutex);
}

CFReadStreamScheduleWithRunLoop(readStream, backgroundRunLoop, kCFRunLoopCommonModes);

CFReadStreamOpen(readStream);

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
CFRelease(url);
CFRelease(urlString);
CFRelease(method);
CFRelease(request);
CFRelease(headerName);
CFRelease(headerValue);
CFRelease(jsonBody);
CFRelease(jsonData);
return YES;
}



连接过去和未来,我们为此而存在
阅读(1986) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~