2011-12-07 56 views
0

現在我想獲得響應正文。我是IOS開發人員中的新人。我只知道我可以使用response.statusCode獲取httpstatuscode,比如400,500等。但如何得到響應的身體。也許allheaderFileds或數據或error.Description?如何獲得IOS中的響應體?

+0

你用什麼API來獲取狀態碼? – ThomasW

+0

in didReceiveResponse並使用response.statusCode – user1084961

+0

就像:'if response.status_code == 200:data = xmltodict(response.content,[])''?它在python – Kjuly

回答

1

全部細節,請訪問:URL Loading System Programming Guide

你需要一個對象,它是NSURLConnection的代表。你需要有一個receivedData成員變量,你需要實現委託方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // Append the new data to receivedData. 
    // receivedData is an instance variable declared elsewhere. 
    [receivedData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // do something with the data 

    // release the connection, and the data object 
    [connection release]; 
    [receivedData release]; 
}