首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用NSURLRequest请求

使用NSURLRequest请求
EN

Stack Overflow用户
提问于 2012-04-10 05:36:50
回答 2查看 39K关注 0票数 10

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

现在我正在尝试使用苹果提供的应用程序接口: NSURLRequest / NSURLConnection等...

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

代码语言:javascript
运行
复制
- (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所需的委托

代码语言:javascript
运行
复制
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

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

- (void)connection:(NSURLConnection *)connection

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

这是我的php代码:

代码语言:javascript
运行
复制
<?php
if(isset($_REQUEST["myVariable"])) {
    echo $_REQUEST["myVariable"];
}
else    echo '$_REQUEST["myVariable"] not found';
?>

那么到底出了什么问题呢?当我启动应用程序时,它将立即崩溃,并显示以下输出:

**

代码语言:javascript
运行
复制
**> 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**

**

我猜,这意味着这一行出了问题:

代码语言:javascript
运行
复制
[request setValue:@"Hello world !" forKey:@"myVariable"];

如果我注释这一行,它实际上是有效的。

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

谢谢你的帮助。

顺便说一句,我对服务器,PHP等知识了解很少。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-10 05:42:06

试试这个:

代码语言:javascript
运行
复制
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

票数 15
EN

Stack Overflow用户

发布于 2013-07-29 17:51:51

试试下面的代码,它是使用web服务(Json)的一种简单方法。

代码语言:javascript
运行
复制
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);
   }
 }
}

希望这能有所帮助。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10080216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档