2012-01-10 39 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];