【问题标题】:Request with NSURLRequest使用 NSURLRequest 请求
【发布时间】:2012-04-09 21:36:50
【问题描述】:

我正在尝试使用数据库(实际上在我的本地主机中)的应用程序,我尝试使用 ASIHTTPRequest 但在 iOS 5 上遇到了很多麻烦(我在那里学会了如何使用 ASIHTTPRequest 表单:http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

现在我正在尝试使用 Apple 提供的 API:NSURLRequest / NSURLConnection 等,...

我阅读了 Apple 在线指南并编写了第一个代码:

- (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?

感谢您的帮助。

附:顺便说一句,我对服务器、PHP 等知之甚少……

【问题讨论】:

    标签: objective-c ios nsurlconnection nsurlrequest


    【解决方案1】:

    试试这个:

    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

    【讨论】:

    • 感谢您的快速回答!不幸的是,这不起作用:编译前出现错误:“NSURLRequest 没有可见的@interface 声明选择器“setHTTPMethod”:/
    • 将您的请求更改为 NSMutableURLRequest,也编辑了我的答案
    • 好吧,应用程序没有崩溃!现在我如何才能访问我的服务器的响应?我认为这是在receiveData 中?我应该向他发送什么消息以恢复我的“Hello world”?
    • 如果请求有效并且您在 php 脚本中看到它,您应该将此问题标记为已回答并打开另一个专门针对 php 以及如何返回所需数据的问题。
    • 这对我来说看起来很神奇(例如 asm( "nop" ) 应该实现相同的效果): if (theConnection) { receiveData = [NSMutableData data]; }
    【解决方案2】:

    试试下面的代码,这是使用网络服务(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);
       }
     }
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2012-02-24
      • 1970-01-01
      相关资源
      最近更新 更多