0

我正在尝试将Wufoo API集成到 iPhone 应用程序中,并且在最后阶段遇到了困难。我已使用AFNetworking成功连接到 Wufoo 表单,并创建了 AFHTTPClient 的子类以容纳所需的额外标头,例如 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;
    }
)

类文件:

视图控制器.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]];
}

日记形式.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;
}

@结尾

4

1 回答 1

1

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

这:

self.parameterEncoding = AFJSONParameterEncoding;

到:

 self.parameterEncoding = AFFormURLParameterEncoding;

Wufoo 通过发送 HTTP POST 数据来工作,然后向您发送 json 或 xml 响应。我所做的是将发送的数据设置为 JSON 数据,而不是应有的表单数据。

希望对尝试做同样事情的人有所帮助:)

于 2013-02-15T01:43:52.243 回答