Iphone 无法从目标c中的NSUrl获取数据

Iphone 无法从目标c中的NSUrl获取数据,iphone,ios,ipad,nsurl,fetch,Iphone,Ios,Ipad,Nsurl,Fetch,我制作了iPad应用程序,我想从这个url获取数据 http://ipad.idealake.com/stockquote.aspx?id=SBIN 我想把这些数据存储到字符串中 我写了这个,但它不把数据放入字符串中 NSURL *urlq=[NSURL URLWithString:str1]; NSURLRequest *reqq=[NSURLRequest requestWithURL:urlq]; [webViewq loadRequest:reqq]; ma

我制作了iPad应用程序,我想从这个url获取数据

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

我在这段代码中犯了什么错误吗?


提前感谢

将编码更改为
NSUTF8StringEncoding
。它会起作用的

用这个

 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];

使用JSON web服务使用
NSURLConnection
。这可能对您有所帮助

该URL的编码几乎肯定不是UTF-32。此外,请不要在错误返回中输入NULL,并发布问题。我只是用一个有效的错误对象运行了这段代码,它清楚地表明存在编码错误(即URL不返回UTF-32编码的数据)。首先检查错误,然后请求帮助。这是常见的礼节。
-(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);
}