简体   繁体   English

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

[英]iOS post json data to web service and return result

I've got WCF web service which takes email address and returns "True" if that entry doesn't exist in the database or "False" if it exists.When i used fiddler and POST data to service it returns result as expected, when i try to send request from my iPad app it just say "Invalid JSON primitive: Email.". 我有WCF Web服务,它将接收电子邮件地址,如果该条目在数据库中不存在,则返回“ True”;如果存在,则返回“ False”。当我使用提琴手和POST数据进行服务时,它按预期返回结果,当我尝试从iPad应用程序发送请求,只是说“无效的JSON原语:电子邮件。”。

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

JSON for testing: JSON测试:

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

Ios Code: 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);
    }
    ]; 

AFHTTPRequestOperationManager uses AFHTTPRequestSerializer by default. AFHTTPRequestOperationManager默认情况下使用AFHTTPRequestSerializer。 Add

manager.requestSerializer = [AFJSONRequestSerializer new];

After AFHTTPRequestOperationManager's initialisation. AFHTTPRequestOperationManager初始化之后。

Also you can simplify your code: 您也可以简化代码:

   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);
    }
    ]; 

Hope this one helps you 希望这对你有帮助

You are using AFHTTPRequestOperation and not AFJSONRequestOperation. 您正在使用AFHTTPRequestOperation而不是AFJSONRequestOperation。

Also you can use the AFHttpClient directly: 您也可以直接使用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];
}];

I would also suggest creating a creating only one instance of the AFHTTPClient using clientWithBaseURL: method. 我还建议使用clientWithBaseURL:方法创建仅创建一个AFHTTPClient实例。 Also read the documentation on github.com/AFNetworking/AFNetworking it does explain how to correctly use the AFHTTPClient. 还要阅读github.com/AFNetworking/AFNetworking上的文档,它确实说明了如何正确使用AFHTTPClient。 Have a look at the AFAppDotNetAPIClient this will give you a good example on how to use the AFHTTPClient. 看看AFAppDotNetAPIClient,这将为您提供有关如何使用AFHTTPClient的好示例。

After i spent hours on the code, i found another method and also that the link of the web service is case sensitive.I've changed the code and now it's working. 在花了几个小时的代码之后,我发现了另一种方法,并且Web服务的链接区分大小写。我更改了代码,现在它可以正常工作了。 If you are using microsoft products on the server side please be careful not to do the mistakes i made. 如果您在服务器端使用Microsoft产品,请注意不要犯我的错误。

The Code: 编码:

 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);
    }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM