Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何将数组上载到Web服务_Objective C_Xcode_Web Services_Nsurl_Nsurlrequest - Fatal编程技术网

Objective c 如何将数组上载到Web服务

Objective c 如何将数组上载到Web服务,objective-c,xcode,web-services,nsurl,nsurlrequest,Objective C,Xcode,Web Services,Nsurl,Nsurlrequest,我对如何将内容上传到Web服务有一些疑问 我一直在使用它从我的Web服务获取信息: NSString * URLString = [NSString stringWithFormat:@"%@%@", kBaseHost, [NSString stringWithFormat:kWSProjectList, token]]; NSURL *url = [NSURL URLWithString:URLString]; NSURLRequest * request = [NSURLReque

我对如何将内容上传到Web服务有一些疑问

我一直在使用它从我的Web服务获取信息:

    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];
}];

现在我必须做相反的事情,我必须上传信息到网络服务,我不知道如何。。。有人可以给我一些指导吗?

如果您使用的是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
}];
-(void)sendArray{
NSArray*数组=
NSData*post=[NSJSONSerialization dataWithJSONObject:数组选项:NSJSONWriting预打印错误:nil];
NSString*postLength=[NSString stringWithFormat:@“%d”,[post length]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:kServerPath]];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
[请求setHTTPBody:post];
[请求设置缓存策略:NSURLRequestReloadIgnoringLocalCacheData];
[请求setTimeoutInterval:5];
NSOperationQueue*队列=[[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:请求队列:队列完成处理程序:^(NSURResponse*响应,NSData*数据,NSError*错误){
//对返回数据执行任何操作
}];

}

sendSynchronousRequest:inBackgroundWithCompletionHandler:这个方法是什么docs@interfaceNSURLConnection(后台)+(void)sendSynchronousRequest:(NSURLRequest)backgroundithCompletionHandler中的请求:(void(^)(NSURLResponse,NSData*,NSError*)处理程序;