2012-03-30 9 views
0

Ich habe einige Zweifel darüber, wie man etwas auf einen WebService hochladen kann.So laden Sie ein Array zu einem WebService

Ich habe das schon mit nach Informationen von meinem Webservice bekommen:

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

Jetzt muß sie das Gegenteil tun, ich habe die Informationen an die WebService laden, und ich habe keine Ahnung, wie .. Jemand könnte mich ein wenig führen?

+0

Was ist diese Methode, sendSynchronousRequest: inBackgroundWithCompletionHandler: Ich sehe nicht, dass man in der Dokumentation – rdelmar

+0

@interface NSURLConnection (Hintergrund) + (void) sendSynchronousRequest: (NSURLRequest *) Anfrage inBackgroundWithCompletionHandler: (void (^) (NSURLResponse *, NSData *, NSError *)) Handler; – Marnerk

Antwort

0

Sie könnten es so machen, wenn Sie JSON verwenden. Wahrscheinlich möchten Sie eine Fehlerüberprüfung hinzufügen, aber Sie sollten damit beginnen.

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

}