iOS将json数据发布到Web服务并返回结果

时间:2014-07-14 07:55:18

标签: c# ios json wcf afnetworking

我有WCF网络服务,如果数据库中不存在该条目,则返回“True”,如果存在,则返回“False”。当我使用fiddler和POST数据进行服务时,返回结果为预计,当我尝试从我的iPad应用程序发送请求时,它只是说“无效的JSON原语:电子邮件。”。

服务链接: http://www.unluapps.com/Service1.svc/CheckRejected

用于测试的JSON:

{"Email":"test@unluco.com"}

Ios代码:

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   NSError *e = nil;
   NSDictionary *parameters = [NSJSONSerialization JSONObjectWithData: [[NSString stringWithFormat:@"{\"Email\":\"%@\"}",_TxtMail.text]  dataUsingEncoding:NSUTF8StringEncoding]
        options: NSJSONReadingMutableContainers
        error: &e
    ];

   [manager POST:@"http://www.unluapps.com/Service1.svc/CheckRejected" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
       // NSLog(@"JSON: %@", responseObject);

        _receivedData=responseObject;
        //NSLog(@"%@",[responseObject objectForKey:@"CheckifExistsResult"]);
        NSLog(@"%@",[responseObject objectForKey:@"CheckRejectedResult"]);
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }
    ]; 

3 个答案:

答案 0 :(得分:1)

AFHTTPRequestOperationManager默认使用AFHTTPRequestSerializer。添加

manager.requestSerializer = [AFJSONRequestSerializer new];

在AFHTTPRequestOperationManager初始化之后。

您也可以简化代码:

   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   manager.requestSerializer = [AFJSONRequestSerializer new];
   NSDictionary *parameters = @{@"Email": _TxtMail.text};

   [manager POST:@"http://www.unluapps.com/Service1.svc/CheckRejected" parameters:parameters   success:^(AFHTTPRequestOperation *operation, id responseObject) {
       // NSLog(@"JSON: %@", responseObject);

        _receivedData=responseObject;
        //NSLog(@"%@",[responseObject objectForKey:@"CheckifExistsResult"]);
        NSLog(@"%@",[responseObject objectForKey:@"CheckRejectedResult"]);
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }
    ]; 

答案 1 :(得分:1)

希望这个可以帮助你

您正在使用AFHTTPRequestOperation而非AFJSONRequestOperation。

您也可以直接使用AFHttpClient:

NSDictionary *parameter = @{@"Email":self.email.text};
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

[httpClient postPath:@"http://www.unluapps.com/Service1.svc/CheckRejected" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Print the response body in text
    BOOL *success = [[responseObject objectForKey:@"success"] boolValue];
    NSLog(@"Response: %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self handleConnectionError:error];
}];

我还建议使用clientWithBaseURL:方法创建一个AFHTTPClient的实例。 另请阅读github.com/AFNetworking/AFNetworking上的文档,它解释了如何正确使用AFHTTPClient。看看AFAppDotNetAPIClient,这将为您提供有关如何使用AFHTTPClient的一个很好的示例。

答案 2 :(得分:1)

在我花了几个小时代码后,我找到了另一种方法,并且Web服务的链接区分大小写。我已经更改了代码,现在它正在运行。如果您在服务器端使用微软产品,请注意不要犯错误。

守则:

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"http://www.unluapps.com/Service1.svc/checkRejected"
       parameters:@{@"email": _TxtMail.text}
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
           NSLog(@"%@",[responseObject objectForKey:@"CheckRejectedResult"]);

          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@", error);
    }];