我是iPhone的新开发者。我必须从此网址获取名称
任何API网址
在表格视图中..我该怎么做才能帮帮我.. 我通过建立连接尝试这个,但它显示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;
}
答案 0 :(得分:2)
尝试使用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中的数据源
答案 1 :(得分:0)
如果您收到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/Reference/Reference.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的结构,因为您需要在上述函数中标识元素/属性名称。然后,您可以将数据存储在数组中,或者最适合的数据。
您还可以选择使用第三方解析器,这里有各种可用解析器的非常好的解释/比较:http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
希望这有帮助!