简体   繁体   English

JSON数据回复无

[英]json data reply nil

NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"text/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"text/json" forHTTPHeaderField:@"Accept"];

    NSError        *error ;
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSLog(@"jsondata=%@,%@",json_string,response);

2011-02-11 12:01:16.653 mystack[1946:207] jsondata=,(null) 2011-02-11 12:01:16.653 mystack [1946:207] jsondata =,(空)

Your URL string is not valid since it contains blank spaces: & . 您的网址字符串无效,因为其中包含空格: & You need to escape those before creating an NSURL object. 您需要在创建NSURL对象之前对它们进行转义。 Replace: 更换:

NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];

with: 与:

NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

thanks for helping me here is the answer 感谢您的帮助,这就是答案

[request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

the code that i have to change.. 我必须更改的代码。

您应该使用NSLog您的错误变量来查看发生了什么: [error localizedDescription]

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

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