我有一种方法,我在其中调用api并返回这样的字符串
-(NSString *)urlReturn{
NSURL *aUrl = [NSURL URLWithString:@"http://deluxecms.net/dev/api/index.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"method=getDocument&
deviceId=cdad86b3d8bca5c09885af94990eda193c40ab03&
cipher=803ae952ff4a785588397362860d045e&
version=1&lastSync=0";
NSLog(@"postString %@",postString);
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse error:&error];
if(error.domain!=nil)
{
NSString *errorDesc=[[error userInfo] objectForKey:@"NSLocalizedDescription"];
NSLog(@"Response Error === %@",errorDesc);
return nil;
}
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseData
options:0 error:&error];
if (doc == nil) {
return nil;
}
NSArray *partyMembers = [doc nodesForXPath:@"//assets/asset[11]/file" error:nil];
NSLog(@"ASSTES %@",partyMembers);
NSString *urlStringmine;
GDataXMLElement *urlElement = (GDataXMLElement *)[partyMembers objectAtIndex:0];
urlStringmine = urlElement.stringValue;
NSLog(@"MY URL STRING %@",urlString);
return urlStringmine;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
urlString = [self urlReturn];
NSURL *urlPattern = [[NSURL alloc]initWithString:urlString];
UIImage *image = [UIImage imageWithData: [NSData
dataWithContentsOfURL:urlPattern]];
NSLog(@"Imge %@",urlPattern);
[self.imageView setImage:image];
}
然后在viewDidLoad方法中,我从该字符串中生成一个url,最后生成一个图像
从这样的URL
但是当我打印urlString时,我得到这样的确切值
http://deluxecms.net/dev/resource/document/Shop/Deluxe screenshot.JPG
但是当我打印urlPattern时,它始终为null。
我无法理解行为?
请帮我。
最佳答案
在使用此命令之前,请尝试转义字符串:
urlString = [[self urlReturn] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];