这是我的工作(请注意,发送到我的服务器的JSON必须是一个字典,其中包含key = question..ie {:question =>
{dictionary}}}的一个值(另一个字典)):
NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"], [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"], nil];NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil];NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"];NSString *jsonRequest = [jsonDict JSONRepresentation];NSLog(@"jsonRequest is %@", jsonRequest);NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];[request setHTTPMethod:@"POST"];[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];[request setHTTPBody: requestData];NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];if (connection) { receivedData = [[NSMutableData data] retain];}
然后,receivedData由以下方式处理:
NSString *jsonString = [[NSString alloc] initWithdata:data encoding:NSUTF8StringEncoding];NSDictionary *jsonDict = [jsonString JSONValue];NSDictionary *question = [jsonDict objectForKey:@"question"];
这不是100%清楚的,需要重新阅读,但是所有内容都应该在这里,以帮助您入门。据我所知,这是异步的。进行这些调用时,我的UI未被锁定。希望有帮助。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)