我正在学习本教程:http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service .试图建立一个基本的网络服务。好像教程是旧 Material ,ASIHTTPRequest 不再继续。我一直在尝试改用 NSURLRequest。第一个问题,NSURLRequest 是一种非常标准的方法吗?我只想要基本的 GET、POST 等东西,我应该换一种方式吗?
我的代码是:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"We want to unlock for the code %@",self.textField.text);
//Get a device ID, (actually can't do this aymore)
NSString *uniqueIdentifier = @"My iPhone";
NSString *code = self.textField.text;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.madasd.co/promos/"]];
request.HTTPMethod=@"POST";
//Set the header fields
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *myString = [NSString stringWithFormat:@"rw_app_id=1&code=%@&device_id=%@",code,uniqueIdentifier];
NSLog(@"%@",myString);
NSData *requestBodyData = [myString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody=requestBodyData;
//Create url and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conn start];
return TRUE;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);
}
第二个问题,我已经使用 curl 测试了后端,所以我知道它工作正常,但是我得到的响应是“无效请求”,我认为这是因为我发送的字符串不正确。我使用 var 名称和 & 运算符是否正确?对此的任何指示都会很棒!谢谢。 (在 Linode 上运行 LAMP 服务器!)
编辑:
还尝试以 JSON 格式发送:
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSDictionary *mapData = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"rw_app_id",code,@"code",uniqueIdentifier,@"device_id", nil];
NSError *error = nil;
NSData *requestBodyData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
request.HTTPBody=requestBodyData;
仍然出现同样的错误。
最佳答案
几个想法:
不要使用
NSURLConnection
。从 iOS 9 开始,它已被弃用。使用NSURLSession
。参见 Using NSURLSession在 URL 加载系统编程指南中。确定您需要准备的请求类型。您在 header 中指定了
application/xml
,但正在创建application/x-www-form-urlencoded
请求。您的Content-Type
header 必须与您构建HTTPBody
的方式相匹配。您的服务器需要什么类型的请求?
x-www-form-urlencoded
? XML? JSON?此外,您的服务器提供什么类型的响应?
如果构建一个
application/x-www-form-urlencoded
请求(如您的请求正文所建议的),您没有正确地转义这些值(参见 https://stackoverflow.com/a/20398755/1271826 ).如果您使用基于委托(delegate)的
NSURLConnection
或NSURLSession
,您不应该只获取didReceiveData
中的结果。你需要做的是在开始请求之前实例化一个
NSMutableData
;didReceiveData
仅附加到那个NSMutableData
;仅当
connectionDidFinishLoading:
(在NSURLConnection
中)或URLSession:task:didCompleteWithError:
(在NSURLSession
) 被调用,如果您随后使用NSMutableData
。
或者,如果使用基于 block 的
NSURLSession
,这个问题就完全消除了(因为你没有实现任何委托(delegate)方法)。使用基于completionHandler
的NSURLSession
方法要容易得多。如果所有这些都太复杂,您可以考虑使用 AFNetworking's
AFHTTPSessionManager
(但不是AFHTTPRequestOperationManager
)来构建您的请求。它可以让您摆脱正确构建请求、实现委托(delegate)方法等的困境。
关于ios - 使用 NSURLSession 进行 POST,发布变量的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31611340/