Ios NSURLRequest未将值过帐到Codeigniter API

Ios NSURLRequest未将值过帐到Codeigniter API,ios,objective-c,api,codeigniter,Ios,Objective C,Api,Codeigniter,你好 我正在尝试使用基于Codeigniter的API连接iOS并使用NSURLRequest。 API处于调试模式,目前它返回与您发布的json相同的键值对。我已经尝试过通过postman将值发布到链接,它工作正常,但是当我通过iOS应用程序发布时,会收到json响应,但应该包含post值的数组为空 以下是iOS代码片段: NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,service]]

你好

我正在尝试使用基于Codeigniter的API连接iOS并使用NSURLRequest。 API处于调试模式,目前它返回与您发布的json相同的键值对。我已经尝试过通过postman将值发布到链接,它工作正常,但是当我通过iOS应用程序发布时,会收到json响应,但应该包含post值的数组为空

以下是iOS代码片段:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,service]];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

NSString * params = @"authkey=waris";
NSData * postData = [params dataUsingEncoding:NSUTF8StringEncoding];

NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];;
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSLog(@"Posting : '%@'  to %@",params,url);

[connection start];
这是我通过postman(一个用于Chrome的RESTFUL客户端)发布相同参数时的响应

但是,当我从上述iOS代码中查询相同的API时,我得到了以下结果:

{
  data = 0;
  status = 1;
}
在此问题上的任何帮助都将不胜感激

我也有同样的问题(不是CodeIgniter,而是Ruby…) 试试这样,解决了我的问题

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,service]];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

NSDictionary *paramDict = @{@"authkey": @"waris"};
NSError *error = nil;
NSData *postData = [NSJSONSerialization dataWithJSONObject:paramDict options:NSJSONWritingPrettyPrinted error:&error];

if (error)
{
    NSLog(@"error while creating data %@", error);
    return;
}

    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];;
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSLog(@"Posting : '%@'  to %@",params,url);

[connection start];

我最终使用了ASIHttpRequest+SBJson组合,效果非常好


在添加ASIHttpRequest核心类和SBJson类来解析JSON之后,我能够实现我想要的

问题在于,由于您创建连接的方式,它将在您完成配置请求之前立即启动。因此,您正在创建一个可变请求,创建并启动连接,然后尝试修改请求,然后再次尝试启动请求

您可以通过更改以下行来解决此问题:

NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
说:

NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

或者,更简单的方法是,在配置完请求后,只需移动
NSURLConnection
(不带
开始:NO
)的原始实例化,然后完全删除
[connection start]

检查您是否在服务器端实际接收到键/值对是。这才是真正的原因。我没有从iOS接收密钥/值对。当我从任何其他客户端发布相同的密钥/值对时,我会收到它们。虽然响应是从iOS获得的,这意味着URL和连接是正常的。顺便说一句,没有必要设置请求的内容长度头,因为
NSURLConnection
会为您这样做。谢谢,我最终使用了ASIHTTPRequest+SBJson组合,效果非常好!您可以找到一个解决方案,但一般来说,我建议您使用一个积极支持的框架,例如,而不是停止使用ASIHttpRequest。
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];