用的NSURLRequest请求(Request with NSURLRequest)

2019-06-23 11:43发布

我试图做的,它使用一个数据库(实际上是在我的本地主机)的应用程序,我试着用ASIHTTPRequest但与iOS 5有这么多的麻烦(我学会了如何使用ASIHTTPRequest形式有: http://www.raywenderlich.com / 2965 /如何到写的-IOS-APP-该用途-A-Web服务

现在,我试图与苹果公司提供的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"];

如果我评论这一行它的实际工作。

我的问题是:我如何发送DATAS到PHP API,使用的NSURLRequest和NSURLConnexion?

谢谢你的帮忙。

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

Answer 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



Answer 2:

试试下面的代码是简单的方法来使用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);
   }
 }
}

希望这可以帮助。



文章来源: Request with NSURLRequest