Iphone json数据回复无
2011-02-11 12:01:16.653 mystack[1946:207]jsondata=,(空)Iphone json数据回复无,iphone,objective-c,json,ipad,nsurlconnection,Iphone,Objective C,Json,Ipad,Nsurlconnection,2011-02-11 12:01:16.653 mystack[1946:207]jsondata=,(空) 您应该记录错误变量以查看发生的情况:[error localizedDescription]谢谢您的帮助,这是答案 NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1]; NSURL
您应该记录错误变量以查看发生的情况:
[error localizedDescription]
谢谢您的帮助,这是答案
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);
我必须更改的代码。您的URL字符串无效,因为它包含空格:
&
。在创建NSURL
对象之前,需要转义这些。替换:
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
与:
服务器的响应似乎有问题。我建议您这样记录响应的文件长度[response length],以检查NSData实例是否为空。jsondata=,(null),0,Error Domain=NSURLErrorDomain Code=-1000“bad URL”UserInfo=0x4b65150{NSUnderlyingError=0x4b68fd0“bad URL”,NSLocalizedDescription=bad URL}
NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
NSString *urlString =[NSString stringWithFormat:@"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];