gpt4 book ai didi

iphone - 在 Objective C 中将数据发布到 Wufoo 表单

转载 作者:行者123 更新时间:2023-11-29 04:08:01 25 4
gpt4 key购买 nike

我正在尝试集成 Wufoo API进入 iPhone 应用程序,并在最后阶段遇到困难。我用过AFNetworking为了成功连接到 Wufoo 表单,创建了 AFHTTPClient 的子类来容纳所需的额外 header ,例如 HTTP 授权。我还将参数编码设置为 AFJSONParameterEncoding。

当我使用上述内容发出请求时,它成功连接到服务器并发布数据,但由于空白字段而返回错误。我已经在 NSDictionary 中添加了要在请求中传递的字段/键对,但它们的格式一定是错误的,因为它们在另一端没有得到处理。抱歉,我知道这是一个很长的问题,但我们将不胜感激:)。我已附加在控制台中得到的响应以及相关的类文件/方法。

响应:

2013-02-14 12:11:43.053 <AppName>[14750:c07] Success?: 0
Error: Errors have been <b>highlighted</b> below.
Fields: (
{
ErrorText = "This field is required. Please enter a value.";
ID = Field1;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field3;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field222;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field11;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field12;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field220;
}
)

类文件:

ViewController.m

 -(NSDictionary *)getParameters {

NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
emailAddress, @"Field1", //problem
firstName, @"Field3", //problem
lastName, @"Field4",
activityArranged, @"Field10",
evidenceDescription, @"Field222", // problem
startDate, @"Field11", //Problem
endDate, @"Field224",
@"1", @"Field12", //Problem
benefitExplanation, @"Field113",
activityCategory, @"Field116",
webAddress, @"Field219",
@"I Agree", @"Field220", //Problem
nil];

return d;
}

-(void)submitForm {
DiaryForm *df = [[DiaryForm alloc] init];
[df submitForm:self params:[self getParameters]];
}

DiaryForm.m

-(void)submitForm:(id)sender params:(NSDictionary *)params {
WufooAPIClient *client = [WufooAPIClient sharedClient];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
NSURLRequest *req = [client requestWithMethod:@"POST" path:@"entries.json" parameters:params];
AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:req success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"Success?: %@\nError: %@\nFields: %@",[JSON objectForKey:@"Success"], [JSON objectForKey:@"ErrorText"], [JSON objectForKey:@"FieldErrors"]);


}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];

NSLog(@"[Error]: (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);

}];
[op start];

}

WufooAPIClient.m

#import "WufooAPIClient.h"
#import "AFNetworking.h"


@implementation WufooAPIClient

+(WufooAPIClient *)sharedClient {
static WufooAPIClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSString *kProtocol = @"https";
NSString *kSub = @"<removed>";
NSString *kHost = @"wufoo.com";
NSString *kHash = @"<removed>";
NSString *sURL = [NSString stringWithFormat:@"%@://%@.%@/api/v3/forms/%@/", kProtocol, kSub, kHost, kHash];
NSLog(sURL);
NSURL *url = [NSURL URLWithString:sURL];
_sharedClient = [[self alloc] initWithBaseURL:url];
});
return _sharedClient;
}

-(id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
[self setAuthorizationHeaderWithUsername:@"<removed>" password:@"<removed>"];
self.parameterEncoding = AFJSONParameterEncoding;

return self;
}

@结束

最佳答案

通过更改 WufooAPIClient.m 类 initWithBaseURL: 方法中的一行解决了该问题。

这个:

self.parameterEncoding = AFJSONParameterEncoding;

致:

 self.parameterEncoding = AFFormURLParameterEncoding;

Wufoo 的工作原理是发送 HTTP POST 数据,然后向您发送回 json 或 xml 响应。我所做的是将发送的数据设置为 JSON 数据,而不是应有的 Form Data。

希望能帮助任何尝试做同样事情的人:)

关于iphone - 在 Objective C 中将数据发布到 Wufoo 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com