请求NSURLRequest [英] Request with NSURLRequest

查看:201
本文介绍了请求NSURLRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

    }

}

我添加了API所需的代表

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

这是我的php代码:

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

我的问题是:如何使用NSURLRequest和NSURLConnexion将数据发送到PHP API?

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

感谢您的帮助。

PS顺便说一句,我对服务器,PHP等知识很差......

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

推荐答案

试试这个:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆