2008-11-05 8 views

Respuesta

30

Este es el código que trabajan por solicitud posterior JSON, TouchJSON marco se utiliza para analizar el JSON, gracias '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"]; 
+1

¿Cuál es el propósito del diccionario "otherRequest"? Parece estar sin usar. –

+0

Ha sido eliminado. –

3

No estoy seguro de cuál es exactamente tu pregunta. Pero google "TouchJSON" que debería ayudarte a comenzar.

+0

¿Cómo se puede usar TouchJSON para hacer esto? –

0

Lo siento por los errores y pérdidas de memoria, pero ¿qué tal algo como:

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); 
+0

Una solución verdaderamente carbono-ish aquí - no va a funcionar muy bien en el iPhone en absoluto. –

+0

Esa es la base de la Fundación, no de carbono. CFNetwork (que es) funciona bien en iOS. No lo preferiría a una solución Cocoa, pero a primera vista no veo nada malo en este código. Core Foundation es muy útil a veces. –

Cuestiones relacionadas