2010-05-24 61 views
1

我想做一個簡單的網址調用。我無法將字節轉換爲NSString。以下是我的代碼。我在我的控制檯消息中得到這個;從網址收到的數據:†F»∞如何將從URL調用接收的數據轉換爲NSString?

-(void)buttonClicked{ 

NSLog(@"Hello Login clicked..."); 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
request = [[NSMutableURLRequest alloc] init]; 

     [request setURL:[NSURL URLWithString:@"http://www.apple.com/contact/"]]; 
     [request setHTTPMethod:@"GET"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setTimeoutInterval:60.0]; 

     NSError *error; 
     NSURLResponse *response; 
     NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
     NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
     NSLog(@"data received from url: %s", data); 

} 

回答

2

請勿在登錄時使用%s修飾符。這是爲了打印char*字符串,其中NSString最肯定是而不是。改爲使用%@修飾符。這特別適用於打印Objective-C對象(如NSString)。

+0

非常感謝,你救了我的一天。 – dipu 2010-05-24 17:26:56

+1

現在,接受他的回答。 ;) – Wevah 2010-05-24 17:32:29