如何将数组上传到 WebService [英] How to upload an array to a WebService
本文介绍了如何将数组上传到 WebService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对如何将内容上传到 WebService 有一些疑问.
I'm having some doubts about how to upload something to a WebService.
我一直在使用它从我的网络服务中获取信息:
I've been using this for getting info from my 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,我不知道如何......有人可以指导我吗?
Now I have to do the opposite, I have to upload the information to the WebService and I have no idea of how... Someone could guide me a little?
推荐答案
如果您使用的是 JSON,则可以这样做.您可能想要添加一些错误检查,但这应该会让您开始.
You could do it like this if you're using JSON. You'd probably want to add some error checking, but this should get you started.
-(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
}];
}
这篇关于如何将数组上传到 WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文