2012-01-10 37 views
-1

我做了iPad应用,我想从这个网址提取数据从NSURL获取数据,无法在目标C

http://ipad.idealake.com/stockquote.aspx?id=SBIN 

,我想存储这些数据转换为字符串,

,所以我写这一点,但它没有考虑到的数据串,日志

NSURL *urlq=[NSURL URLWithString:str1]; 
    NSURLRequest *reqq=[NSURLRequest requestWithURL:urlq]; 
    [webViewq loadRequest:reqq]; 


mainstr=[[NSMutableString alloc] initWithContentsOfURL:urlq encoding:NSUTF32StringEncoding error:NULL]; 

    NSLog(@"WHOLE STRING=%@",mainstr); 

输出是:整个字符串= NULL

我在做这个代码中的任何错误?

在此先感谢!

+2

该URL的编码几乎可以肯定*不是* UTF-32 – 2012-01-10 04:48:17

+0

也请在您的错误返回中输入NULL并发布问题。我只是用一个有效的错误对象运行这段代码,它清楚地表明存在编码错误(即,URL不返回UTF-32编码数据)。先检查你的错误,然后请求帮助。这很常见。 – 2012-01-10 04:50:25

回答

0
-(void)fire{ 

NSString *URL = @"http://ipad.idealake.com/stockquote.aspx?id=SBIN"; 
    NSURLRequest *rq = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]]; 
    conn1 = [[NSURLConnection alloc] initWithRequest:rq delegate:self]; 
    webData = [[NSMutableData data] retain]; 
} 


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [webData setLength: 0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [webData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"ERROR with theConenction"); 
    [connection release]; 
    [webData release]; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *theResult = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"xml : %@",theResult); 
} 
0

将编码改为NSUTF8StringEncoding。它会工作。

0

使用使用JSON的Web服务这

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:str1]]; 
     NSData *myData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil]; 
     NSString *finalRespStr = [[[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding] autorelease];