[英]how to get the data from url in iphone
我是 iPhone 的新開發者。我必須從這個 URL 中獲取名稱
任意 API URL
在表格視圖中..我該怎么做,請幫助我..我通過建立連接來嘗試這個,但它顯示 HTTP 超時錯誤。這是什么原因,還有其他方法可以獲取數據。 .
這是我的連接代碼...
static NSString *feedURLString =
> @"http://www.XXXXX.com/XXXXXX/api/XXXXX.php?method=All";
> NSURLRequest *studentURLRequest =[NSURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];
>self.studentFeedConnection =[[[NSURLConnection alloc] initWithRequest:studentURLRequest
> delegate:self] autorelease];
NSAssert(self.studentFeedConnection != nil, @"Failure to create URL connection.");
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
> [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addstudent:) name:kAddstudentNotifm object:nil];
>[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(studentError:) name:kstudentErrorNotif object:nil];
> - (void)connection:(NSURLConnection *)connection
> didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
> if ((([httpResponse statusCode]/100) == 2) && [[response MIMEType]
> isEqual:@"application/atom+xml"]) {
> self.studentData = [NSMutableData data];
> }
else {
> NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"HTTP Error", @"Error message
> displayed when receving a connection error.")
>
> forKey:NSLocalizedDescriptionKey];
NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode]userInfo:userInfo];
> [self handleError:error];
>}
}
>
> - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
> [studentData appendData:data];
}
>
> - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
> [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
> if ([error code] == kCFURLErrorNotConnectedToInternet) {
> // if we can identify the error, we can present a more precise message to the user.
> NSDictionary *userInfo = [NSDictionary dictionaryWithObject: NSLocalizedString(@"No Connection Error", @"Error message displayed when not connected to the Internet.")forKey:NSLocalizedDescriptionKey];
>NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
> [self handleError:noConnectionError];
>}
else {
>// otherwise handle the error generically
>[self handleError:error];
>}
> self.studentFeedConnection = nil;
}
嘗試使用 NSXMLParser
NSString *site = [NSString stringWithString:
@"http://www.XXXXX.com/XXX/api/XXXX.php?method=All"];
NSURL *url = [NSURL URLWithString:site];
NSXMLParser *myparser = [NSXMLParser initWithContentsOfURL:url];
myparser.delegate = self;
[myparser parse];
確保執行
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
- (void) parserDidEndDocument:(NSXMLParser *)parser;
將數據解析為 NSArray 或 NSDictionary 后,您可以將其用作 UITableView 中的數據源
如果您收到 HTTP 錯誤,那么您的連接方式很可能有問題。 您的連接是否需要身份驗證? 如果是這樣,您將需要實施:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *myCreds = [[NSURLCredential alloc] initWithUser:@"**USERNAME**" password:@"**PASSWORD**" persistence:NO];
[challenge.sender useCredential:myCreds forAuthenticationChallenge:challenge];
[myCreds release];
}
至於實際檢索數據,如您需要使用 NSXML Parser,您可以在此處查看詳細信息: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class /參考/參考.html 。 您必須實現的只有 3 個方法,其余的只是獎勵。 這三個是
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
理想情況下,您知道您正在解析的 XML 的結構,因為您需要識別上述函數中的元素/屬性名稱。 然后,您可以將數據存儲在數組中,或者任何最適合的方式。
您還可以選擇使用第 3 方解析器,這里有各種可用解析器的很好的解釋/比較: http://www.raywenderlich.com/553/how-to-chose-the-best-xml -parser-for-your-iphone-project
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.