2012-04-09 109 views
10

我試圖做的,它使用一個數據庫(實際上是在我的本地主機)的應用程序,我試着用ASIHTTPRequest但與iOS 5有這麼多的麻煩(我學會了如何使用ASIHTTPRequest形式有:http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service請求與的NSURLRequest

現在我用蘋果提供的API嘗試:的NSURLRequest/NSURLConnection的等等,...

我讀了蘋果的在線指南,使這首代碼:

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

    } 

} 

我添加所需的代表由API

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

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

- (void)connection:(NSURLConnection *)connection 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

這裏是我的PHP代碼:

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

那麼,什麼是錯的?當我啓動應用程序,它會立即崩潰與此輸出:

**

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

**

我猜,這意味着有些事情是錯的這條線:

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

它實際上如果我評論這一行的作品。

我的問題是:如何使用NSURLRequest和NSURLConnexion將數據發送到PHP API?

謝謝你的幫助。

P.S.順便說一句,我對服務器的知識差,PHP ECT,...

回答

14

試試這個:

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

} 

這裏看到https://stackoverflow.com/a/6149088/1317080

+0

感謝您的快速回答!不幸的是,這不起作用:編譯前我有一個錯誤:「NSURLRequest沒有明顯的@interface聲明選擇器」setHTTPMethod「:/ – Edelweiss 2012-04-09 21:58:05

+1

更改您的請求到一個NSMutableURLRequest,編輯我的答案以及 – 2012-04-09 22:06:26

+1

嗯,應用程序不會崩潰!現在我怎樣才能訪問我的服務器的響應?我認爲這是在receiveData中?我應該發送給他什麼信息讓我的「Hello world」返回? – Edelweiss 2012-04-09 22:23:09

4

嘗試下面的代碼是簡單的方法來一個消費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); 
    } 
} 
} 

希望這有助於。