早安,
我正在尝试使用基于 Codeigniter 的 API 来连接 iOS 并使用 NSURLRequest。 API 处于 Debug模式,现在它返回与您发布的 json 相同的键值对。我尝试通过 postman 将值发布到链接并且它工作正常,但是当我通过我的 iOS 应用程序发布它时,收到了 json 响应,但应该包含发布值的数组是空的。
这是 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"OST"];
[request setHTTPBody:postData];
NSLog(@"osting : '%@' to %@",params,url);
[connection start];
这是我通过 postman (A RESTFUL Client for Chrome)发布相同参数时的响应
{
"status": "1",
"data": {
"authkey": "warisali"
}
}
但是,当我从上面的 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:@"OST"];
[request setHTTPBody:postData];
NSLog(@"osting : '%@' to %@",params,url);
[connection start];
关于ios - NSURLRequest 未将值发布到 Codeigniter API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25261332/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |