Json Code无效

时间:2016-11-28 07:48:48

标签: ios objective-c json parsing post

NSString *urlString = @"http://chkdin.com/dev/api/peoplearoundmexy/?";
NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

NSString *parameterString=[NSString stringWithFormat:@"skey=%@&user_id=%@",@"XXXXXXX",@"3225"];


NSLog(@"%@",parameterString);


[request setHTTPMethod:@"POST"];

[request setURL:url];

[request setValue:parameterString forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


NSData *postData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:postData];

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]);
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"%@",dict);

这是我的json解析,我的问题是当我遇到这个api它正在显示

{
  message = "Valid skey required.";
status = 0;
}

但是这个api正在safari中工作。我觉得问题是请求添加到url错误。你可以帮我吗....

2 个答案:

答案 0 :(得分:1)

我通过AFNetworking 3获得了回复

试试这个

z-index:-2

enter image description here

答案 1 :(得分:1)

我尝试使用不带AFNetworking的代码并且工作正常。

NSString *post = [NSString stringWithFormat:@"skey=%@&user_id=%@",@"sa6rw9er7twefc9a7dvcxcheckedin",@"3225"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://chkdin.com/dev/api/peoplearoundmexy/?%@",post]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:nil];
NSError *error;

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]);
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",dict);