2016-11-28 43 views
2
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=%@",@"sa6rw9er7twefc9a7dvcxcheckedin",@"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,它顯示Json的代碼是不能工作

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

但是這個API是工作在safari.i我覺得是問題是爲請求添加到URL錯誤。你能幫助我,請....

+0

你遇到了什麼問題 –

+0

結果顯示是有效的密鑰必需的,但skey在url中工作。 –

+0

http://chkdin.com/dev/api/peoplearoundmexy/?skey=sa6rw9er7twefc9a7dvcxcheckedin&user_id=3225 –

回答

1

我通過AFNetworking 3

試試這個

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
    manager.responseSerializer = [AFJSONResponseSerializer serializer]; 
    [manager GET:@"http://chkdin.com/dev/api/peoplearoundmexy/?" parameters:@{@"skey":@"sa6rw9er7twefc9a7dvcxcheckedin",@"user_id":@"3225"} progress:nil success:^(NSURLSessionTask *task, id responseObject) { 
     NSLog(@"%@",responseObject); 
    } failure:^(NSURLSessionTask *operation, NSError *error) { 
    }]; 

enter image description here

+0

你可以發佈完整的代碼請。 –

+0

這是完整的代碼。 – KKRocks

+0

通過此網址下載afnetworking庫https://github.com/AFNetworking/AFNetworking – KKRocks

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); 
+0

謝謝bro這個代碼也在工作。 –

+0

歡迎你。 – Mahesh