请求NSURLRequest

Ede*_*iss 10 objective-c nsurlconnection nsurlrequest ios

我正在尝试使用数据库的应用程序(实际上在我的本地主机中),我尝试使用ASIHTTPRequest但是在iOS 5中遇到了很多麻烦(我在那里学习了如何使用ASIHTTPRequest表单:http://www.raywenderlich.com / 2965 /如何到写的-IOS-APP-该用途-A-Web服务

现在我正在尝试使用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];   

    }

}
Run Code Online (Sandbox Code Playgroud)

我添加了API所需的代理

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

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

- (void)connection:(NSURLConnection *)connection

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
Run Code Online (Sandbox Code Playgroud)

这是我的PHP代码:

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

那有什么不对?当我启动应用程序时,它将立即崩溃此输出:

**

**> 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**
Run Code Online (Sandbox Code Playgroud)

**

我想,这意味着这条线上的某些东西是错误的:

[request setValue:@"Hello world !" forKey:@"myVariable"];
Run Code Online (Sandbox Code Playgroud)

如果我评论这一行,它确实有效.

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

谢谢你的帮忙.

PS顺便说一下,我对服务器,PHP等方面的知识很差......

Mir*_*ner 15

试试这个:

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

}
Run Code Online (Sandbox Code Playgroud)

在这里看到/sf/answers/430436191/


Dur*_*n.H 5

尝试下面的代码,这是使用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);
   }
 }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。