Comment faire une demande de publication HTTP pour obtenir un objet JSON en réponse pour une application iPhone?

StackOverflow https://stackoverflow.com/questions/264140

Question

J'ai un service Web fonctionnant sur le serveur qui renvoie les données au format XML ou JSON. Je voulais demander un format JSON mais en utilisant la méthode HTTP Post.

toute aide grandement appréciée.

merci d'avance.

Était-ce utile?

La solution

C’est le code qui fonctionne pour la demande de publication JSON, TouchJSON Framework est utilisé pour analyser le JSON, merci 'schwa'.

NSArray *keys = [NSArray arrayWithObjects:@"username", @"password", @"preference", @"uid", nil];
NSArray *objects = [NSArray arrayWithObjects:@"accuser", @"accpass", @"abc_region", @"100", nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSURL *theURL = [NSURL URLWithString:@"http://url.com/request.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"];
NSString *theBodyString = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary];
NSLog(@"%@", theBodyString);
NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"%@", theBodyData);
[theRequest setHTTPBody:theBodyData];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *theResponseString = [[[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding] autorelease];
NSLog(theResponseString);
NSDictionary *theResponseDictionary = [[CJSONDeserializer deserializer] deserialize:theResponseString];
NSLog(@"%@", theResponseDictionary);
NSString *theGreeting = [theResponseDictionary objectForKey:@"greeting"];
[self setValue:theGreeting forKey:@"greeting"];

Autres conseils

Je ne sais pas trop quelle est votre question. Mais google " TouchJSON " cela devrait vous aider à démarrer.

Désolé pour les erreurs et les fuites de mémoire, mais que dire de quelque chose comme:

CFURLRef url = CFURLCreateWithString(NULL, CFSTR("http://example.com/post"), NULL);
CFHTTPMessageRef msg = CFHTTPMessageCreateRequest(
    NULL,
    CFSTR("POST"),
    url,
    kCFHTTPVersion1_1);

const char *body = "key=value&id=30293";
CFDataRef bodyData = CFDataCreate(NULL, body, strlen(body));
CFHTTPMessageSetBody(msg, bodyData);

CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(NULL, myRequest);
CFReadStreamOpen(myReadStream);
CFHTTPMessageRef myResponse = CFReadStreamCopyProperty(
    myReadStream,
    kCFStreamPropertyHTTPResponseHeader);

//
// Handle myResponse
//

CFReadStreamClose(myReadStream);
CFRelease(myReadStream);
CFRelease(bodyData);
CFRelease(msg);
CFRelease(url);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top