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

时间:2012-01-10 04:45:08

标签: iphone ios ipad nsurl fetch

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

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

日志输出为:WHOLE STRING = NULL

我在这段代码中有任何错误吗?

提前致谢!!

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

使用此

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

答案 2 :(得分: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);
}

答案 3 :(得分:0)

使用JSON Web服务使用NSURLConnection。它可能对你有所帮助how to download the data from web service