AFNetworking POST到REST webservice
简介背景故事,我们以前的开发人员使用ASIHTTPRequest来发出POST请求并从我们的web服务中检索数据。原因未知我们的应用程序的这部分停止工作。似乎有足够的时间来证明未来,并与AFNetworking合作。 REST webservice在CakePHP框架上运行。AFNetworking POST到REST webservice
总之,我没有收到使用AFNetworking的请求响应字符串。
我知道web服务工作,因为我能够成功发布的数据,并使用得到适当的响应卷曲: 卷曲-d“数据[产品型号] [field0] = field0value &数据[产品型号] [字段1] = field1value” https://example.com/api/class/function.plist
根据以前的开发人员的指示,我想出了以下内容。
#import "AFHTTPRequestOperation.h"
…
- (IBAction)loginButtonPressed {
NSURL *url = [NSURL URLWithString:@"https://example.com/api/class/function.plist"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[usernameTextField text] forHTTPHeaderField:@"data[User][email]"];
[request setValue:[passwordTextField text] forHTTPHeaderField:@"data[User][password]"];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
NSLog(@"response string: %@ ", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
}];
[operation start];
}
输出: 操作hasAcceptableStatusCode:200 响应字符串:空白plist文件
尝试的解决方案1: AFNetworking Post Request 所提出的解决方案使用AFHTTPRequestOperation的函数调用operationWithRequest。但是,当我尝试使用所述解决方案时,我收到警告“类方法”+ operationWithRequest:完成:'未找到(返回类型默认为'id'“
尝试解决方案2:NSURLConnection。output:打印传递消息的成功日志,但不响应字符串 *更新 - 。返回空白的plist
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *httpBodyData = @"data[User][email][email protected]&data[User][password]=awesomepassword";
[httpBodyData dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[NSData dataWithContentsOfFile:httpBodyData]];
NSHTTPURLResponse __autoreleasing *response;
NSError __autoreleasing *error;
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
// *update - returns blank plist
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseData %@",str);
if (error == nil && response.statusCode == 200) {
// Process response
NSLog(@"success");//returns success code of 200 but blank
NSLog(@"resp %@", response);
} else {
// Process error
NSLog(@"error");
}
这些是最终满足我对Web服务请求的基本要求(剥离条件是我为自己使用的)。感谢@ 8vius和@mattt的建议!
- (IBAction)loginButtonPressed {
NSURL *baseURL = [NSURL URLWithString:@"https://www.example.com/api/class"];
//build normal NSMutableURLRequest objects
//make sure to setHTTPMethod to "POST".
//from https://github.com/AFNetworking/AFNetworking
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient defaultValueForHeader:@"Accept"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
[usernameTextField text], kUsernameField,
[passwordTextField text], kPasswordField,
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"https://www.example.com/api/class/function" parameters:params];
//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc]
initWithRequest:request] autorelease];
//"Why don't I get JSON/XML/Property List in my HTTP client callbacks?"
//see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
//mattt's suggestion http://stackoverflow.com/a/9931815/1004227 -
//-still didn't prevent me from receiving plist data
//[httpClient registerHTTPOperationClass:
// [AFPropertyListParameterEncoding class]];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation,
id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: [%@]",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
}];
//call start on your request operation
[operation start];
[httpClient release];
}
使用AFHTTPClient -postPath:parameters:success:failure:
,通过你的参数(嵌套词典/阵列罚款)如果你希望一个plist中回来。 ,一定要有客户端注册AFPropertyListRequestOperation
在任何情况下,setValue:forHTTPHeaderField:
不是你想要的。 HTTP headers用于指定关于请求本身的信息;数据是请求主体的一部分。 AFHTTPClient
自动将参数转换为GET
请求的查询字符串或POST
等的HTTP主体。
我解决了这个问题,但我有一种感觉,我仍然在做错误或效率低下的事情。如果您快速查看我的解决方案,我将不胜感激。谢谢! – Airuop 2012-04-03 21:11:21
你好,Pouria,你尝试使用NSURLConnection发送同步请求吗? – 8vius 2012-03-29 15:17:14
尝试解决方案2:我是如何尝试NSURLConnection。 – Airuop 2012-03-29 17:20:56
服务器端的所有设置是否正确?像扩展解析一样,响应的布局和它们的视图?看起来更像是一个服务器问题,而不是客户端问题。 – 8vius 2012-03-29 19:08:18