如何测试json数据是否到达服务器ios

时间:2015-09-05 08:58:19

标签: ios json

我试图将一些json数据发送到我的ios应用中的http服务器,但我不知道如何验证数据是否到达 ...使用http://www.jsontest.com/进行测试。

NSURL *url = [NSURL URLWithString:@"http://validate.jsontest.com"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:createJson()];      //createJson() is my function which pass json data
                                              encoding:NSUTF8StringEncoding]);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://validate.jsontest.com"]]; //by this I want to open json site in browser and see if data come. - but something different should be here I guess

那么......我应该如何更改代码以在浏览器中查看createJson()中的数据?代码中是否有任何错误,因此数据甚至不会发送到服务器? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我建议使用AFNetworking

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

你有2个街区,一个成功运行&其他失败。

这是Github上的链接 https://github.com/AFNetworking/AFNetworking