Request with NSURLRequest
Asked Answered
C

2

10

I'm trying do to an app which uses a database (actually in my localhost), I tried with ASIHTTPRequest but having so much troubles with iOS 5 (I learnt how to use ASIHTTPRequest form there : http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

Now i'm trying with the API provided by Apple : NSURLRequest / NSURLConnection etc,...

I read the Apple online guide and make this first code :

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

    }

}

I added the delegates needed by the API

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

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

- (void)connection:(NSURLConnection *)connection

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

Here is my php code :

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

So what is wrong? When I start the app, it will immediately crash with this output :

**

**> 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**

**

I guess, it means that somethings is wrong with this line :

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

It actually works if I comment this line.

My question is : How can I send datas to a PHP API, using NSURLRequest and NSURLConnexion?

Thank you for helping.

P.S. By the way, I have poor knowledges about server, PHP ect,...

Chandelier answered 9/4, 2012 at 21:36 Comment(0)
S
15

try this:

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

}

seen here https://mcmap.net/q/239167/-append-data-to-a-post-nsurlrequest

Spectacle answered 9/4, 2012 at 21:42 Comment(5)
Thank you for your quick answer ! Unfortunately this does not work : I have an error before compilation : "No visible @interface for NSURLRequest declares the selector "setHTTPMethod" : /Chandelier
change your request to a NSMutableURLRequest, edited my answer as wellSpectacle
Well, the app does not crash ! Now how can I have access to my server's response? I think this is in the receiveData? What message should I send to him for get my "Hello world" back?Chandelier
if the request is working and if you see it in your php script, you should mark this question as answered and open another one referring specifically to php and how to return the data you want.Spectacle
the end of this looks like magic to me (e.g. asm( "nop" ) should achieve the same): if (theConnection) { receiveData = [NSMutableData data]; }Morrow
L
5

Try the below code it is one of the simple ways to consume a web service(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);
   }
 }
}

Hope this helps.

Loeb answered 29/7, 2013 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.