2011-11-07 16 views
13

bu adrees i bir nesne gönderiyorum böyleOku NSURLresponse

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response :

NSlog(@"%@",response); i Bu kodu alıyorum:

<NSHTTPURLResponse: 0x7d2c6c0> Bir şekilde bir ipucu almam gerek. Nasıl okuyabilirim?

cevap

18

Bu cevabı başka bir soruya yazdım, ama size yardımcı olacağını düşünüyorum. yöntemlerde de özellikle göz

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

ve

-(void) connectionDidFinishLoading:(NSURLConnection *)connection


-(void) requestPage 
{ 
    NSString *urlString = @"http://the.page.you.want.com"; 
    NSURL *url = [NSURL URLWithString:urlString]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f]; 


    responseData = [[NSMutableData alloc] init]; 
    connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain]; 
    delegate = target; 
} 


-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) 
    { 
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
     //If you need the response, you can use it here 
    } 
} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
} 

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [responseData release]; 
    [connection release]; 
} 

-(void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    if (connection == adCheckConnection) 
    { 
     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

     //You've got all the data now 
     //Do something with your response string 


     [responseString release]; 
    } 

    [responseData release]; 
    [connection release]; 
} 
+0

veri almaya başlayabileceğinizi, ancak birden çok isteği nasıl yanıtlayacağınızı belirtir. Örneğin bir bağlantı devam ederken ve connectiondidfinishloading önce başka bir istek geliyorsa o zaman ResponseData sağa çıkma şansı var mı? – raghul

+0

@raghul Evet, bu mümkün. Bunu engellemek için, muhtemelen bir "ConnectionHandler" sınıfında sardım, böylece her bir bağlantının kendi "responseData" örneğine sahip olması gerekirdi. –

6
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response; 
int errorCode = httpResponse.statusCode; 
NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString]; 

Daha fazla bilgi için, bkz. IOS Docs: NSHTTPURLResponse.

Ve sabırlı olun: tüm bağlantıları NSHTTPURLResponse

+0

i 200 wich Tamam alıyorum ... ama nasıl ben elma sunucu okuyabilir tepki? –

+0

200, bağlantınızın doğru bir şekilde kurulduğunu ve – Nekto

1

Sen bir alt sınıfını oluşturup - (NSString*) description yöntemi geçersiz kılabilirsiniz dönün.

3

Bağlantının bazı verileri almasını bekliyorsanız, kullanabilirsiniz.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

sonra basitçe NSString verileri dönüştürebilir.