2012-03-30 35 views
0

我對如何將某些內容上傳到WebService存在一些懷疑。如何將數組上傳到WebService

我一直在使用這個從我的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]; 
}]; 

現在我要做相反的,我已經上傳信息到WebService的,我不知道該怎樣想法..有人可以指導我一點嗎?

+0

這是什麼方法,sendSynchronousRequest:inBackgroundWithCompletionHandler:我沒有看到一個在文檔 – rdelmar 2012-03-30 18:44:45

+0

@interface NSURLConnection的(背景) +(無效)sendSynchronousRequest:(*的NSURLRequest)請求inBackgroundWithCompletionHandler:(void(^)(NSURLResponse *,NSData *,NSError *))handler; – Marnerk 2012-04-03 17:19:10

回答

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

}

相關問題