开发者

how to get the data from url in iphone

i am a new developer of iPhone.I have to get the names from this URL

Any API URL

in the table view.. how can i do this please help me.. i am try this by making connection but it is showing HTTP time out error .what is the reason by that and is there is any other way to get the data..

here is my connection code...

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;
}


Try using 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];

Make sure to implement

- (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;

Once you have the data parsed into an NSArray or NSDictionary you can use it as a data source in the UITableView


If you are getting an HTTP error, than chances are there is something wrong with the way in which you connect. Does your connection require authentication? If so you will need to implement:

- (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];
}

As for actually retrieving the data like you will need to use NSXML Parser, you can see the details for it here: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html. There are only 3 methods that you MUST implement, the remaining are just bonus. The three are

- (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;

Ideally you know the structure of the XML that you are parsing as you will be required to identify the element/attribute name in the above functions. You can then store you data in an array, or whatever fits best.

You also have the option to use a 3rd party parser, there is a very good explanation/comparison of the various available parsers here: http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜