我对如何将某些内容上传到WebService有疑问。
我一直用它从我的网络服务获取信息:
NSString * URLString = [NSString stringWithFormat:@"%@%@", kBaseHost, [NSString stringWithFormat:kWSProjectList, token]];
NSURL *url = [NSURL URLWithString:URLString];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendSynchronousRequest:request inBackgroundWithCompletionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *json = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"%@", json);
NSDictionary * _response = [json JSONValue];
NSLog (@"%@", _response);
NSNotification* notification = [NSNotification notificationWithName:kWSNotificationDidReceiveDataProjectList object:_response];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}];
现在我必须做相反的事情,我必须将信息上传到WebService,我不知道如何......有人可以指导我一点吗?
答案 0 :(得分:0)
如果你使用的是JSON,你可以这样做。你可能想要添加一些错误检查,但这应该让你开始。
-(void)sendArray {
NSArray *array = <your array here>
NSData *post = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kServerPath]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:post];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:5];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* error){
//Do whatever with return data
}];
}