2012-04-09 14 views
10

Sto cercando di fare un app che utilizza un database (in realtà nel mio localhost), ho provato con ASIHTTPRequest ma avendo così tanto problemi con iOS 5 (ho imparato a usare forma ASIHTTPRequest lì: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-servicerichiesta con NSURLRequest

Ora sto cercando con le API fornite da Apple: NSURLRequest/NSURLConnection etc, ...

ho letto la guida online di Apple e fanno di questo primo codice:

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 



    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL 

           URLWithString:@"http://localhost:8888/testNSURL/index.php"] 

           cachePolicy:NSURLRequestUseProtocolCachePolicy 

           timeoutInterval:60.0]; 



    [request setValue:@"Hello world !" forKey:@"myVariable"]; 



    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    if (theConnection) { 

     receiveData = [NSMutableData data]; 

    } 

} 

ho aggiunto i delegati necessari dall'API

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

- (void)connection:(NSURLConnection *)connection 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

Ecco il mio codice php:

<?php 
if(isset($_REQUEST["myVariable"])) { 
    echo $_REQUEST["myVariable"]; 
} 
else echo '$_REQUEST["myVariable"] not found'; 
?> 

Così che cosa è sbagliato? Quando avvio l'applicazione, sarà immediatamente in crash con questa uscita:

**

**> 2012-04-09 22:52:16.630 NSURLconnextion[819:f803] *** Terminating app 
> due to uncaught exception 'NSUnknownKeyException', reason: 
> '[<NSURLRequest 0x6b32bd0> setValue:forUndefinedKey:]: this class is 
> not key value coding-compliant for the key myVariable.' 
> *** First throw call stack: (0x13c8022 0x1559cd6 0x13c7ee1 0x9c0022 0x931f6b 0x931edb 0x2d20 0xd9a1e 0x38401 0x38670 0x38836 0x3f72a 
> 0x10596 0x11274 0x20183 0x20c38 0x14634 0x12b2ef5 0x139c195 0x1300ff2 
> 0x12ff8da 0x12fed84 0x12fec9b 0x10c65 0x12626 0x29dd 0x2945) terminate 
> called throwing an exception** 

**

credo, vuol dire che è sbagliato quarantina con questa linea:

[request setValue:@"Hello world !" forKey:@"myVariable"]; 

In realtà funziona se commento questa riga.

La mia domanda è: come posso inviare i dati a un'API PHP, utilizzando NSURLRequest e NSURLConnexion?

Grazie per l'aiuto.

P.S. A proposito, ho conoscenze poveri circa del server, PHP ecc, ...

risposta

14

provare questo:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL 

          URLWithString:@"http://localhost:8888/testNSURL/index.php"] 

          cachePolicy:NSURLRequestUseProtocolCachePolicy 

          timeoutInterval:60.0]; 

[request setHTTPMethod:@"POST"]; 
NSString *postString = @"myVariable=Hello world !"; 
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

if (theConnection) { 

    receiveData = [NSMutableData data]; 

} 

visto qui https://stackoverflow.com/a/6149088/1317080

+0

Grazie per la tua rapida risposta! Sfortunatamente non funziona: ho un errore prima della compilazione: "Nessuna interfaccia @ visibile per NSURLRequest dichiara il selettore" setHTTPMethod ":/ – Edelweiss

+1

cambia la tua richiesta in NSMutableURLRequest, ha modificato anche la mia risposta –

+1

Bene, l'app non si blocca! Ora, come posso accedere alla risposta del mio server? Penso che questo sia nel receiveData? Quale messaggio dovrei inviare a lui per ottenere il mio "Hello world"? – Edelweiss

4

Prova il codice qui sotto è uno dei modi semplici per consumare un servizio web (json)

NSURL *url = [NSURL URLWithString:@"yourURL"]; 

NSMutableURLRequest *urlReq=[NSMutableURLRequest requestWithURL:url]; 

NSURLResponse *response; 

NSError *error = nil; 

NSData *receivedData = [NSURLConnection sendSynchronousRequest:urlReq 
               returningResponse:&response 
                 error:&error]; 
if(error!=nil) 
{ 
    NSLog(@"web service error:%@",error); 
} 
else 
{ 
if(receivedData !=nil) 
{ 
    NSError *Jerror = nil; 

    NSDictionary* json =[NSJSONSerialization 
         JSONObjectWithData:receivedData 
         options:kNilOptions 
         error:&Jerror]; 

    if(Jerror!=nil) 
    { 
    NSLog(@"json error:%@",Jerror); 
    } 
} 
} 

Spero che questo aiuti.

Problemi correlati