objective-c – 请求与NSURLRequest

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

现在我正在尝试使用Apple提供的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?

感谢您的帮助.

附:顺便说一句,我对服务器,PHP等的知识差

解决方法

尝试这个:
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

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...