Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2009, 02:54 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 21
Alex1987 is on a distinguished road
Default Need help with getting data from the web

Hi guys,

I'm trying to get the contents of a html file (I get it as NSData) and extract some information from it. Now my approach was to convert it to CString like that:

NSString *respData = [NSString stringWithCString:[theData bytes] length:[theData length]];

Then I parse that string to get what I need. Now, it works fine when it encounters Latin characters but when there are Cyrilic or Chinese characters for example it won't recognize and display jibrish.... What can be done?

Will converting my NSData to UTF8 help? How can it be done when an HTML file contain chars like " ?

Thanks
Alex1987 is offline   Reply With Quote
Old 09-09-2009, 03:18 PM   #2 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Dutch will become famous soon enough
Send a message via AIM to Dutch
Default

This is how I do it. This is asynchronous so it will not tie up the main thread.

.h file
Code:
NSURLConnection *connection;
NSMutableData* data;

-(void)FetchHTMLCode:(NSString *)url;

@property (nonatomic, retain) NSURLConnection *connection;
@property (nonatomic, retain) NSMutableData* data;
.m file
Code:
-(void)FetchHTMLCode:(NSString *)url{
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];	
	
}

- (void)connection:(NSURLConnection *)theConnection	didReceiveData:(NSData *)incrementalData {
    if (data==nil) data = [[NSMutableData alloc] initWithCapacity:2048];
    [data appendData:incrementalData];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

     //ASCII Method
    NSString *result= [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    //UTF8 Method
    //NSString *result= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    //result should now have your finished code. See which encoding method works out the best.

    data=nil;
}
Dutch is offline   Reply With Quote
Old 09-09-2009, 04:31 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 21
Alex1987 is on a distinguished road
Default

Well, I tried the NSASCIIStringEncoding, but it still won't work when using non english letters. Have you tried loading html with russian text for instance ?

The UTF8 is even worse. It return nil or empty string. I think this is becase the html file has non UTF8 characters.

Last edited by Alex1987; 09-09-2009 at 04:35 PM.
Alex1987 is offline   Reply With Quote
Old 09-09-2009, 11:57 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 153
StevenD is on a distinguished road
Default

Dutch,

Where in your code does it command that the acquisition of the data runs asynchronously? And is that synonymous with "in another thread"?
/Steve
StevenD is offline   Reply With Quote
Old 09-10-2009, 06:21 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 21
Alex1987 is on a distinguished road
Default

Ok if no one knows, let me ask something else:
How would you retrieve data from web pages without string parsing? Are there some libraries that deal with that ?
Alex1987 is offline   Reply With Quote
Old 09-10-2009, 07:12 AM   #6 (permalink)
Daddy Cool
iPhone Dev SDK Supporter
 
MarkC's Avatar
 
Join Date: Mar 2009
Location: Yorkshire, England
Age: 100
Posts: 1,616
MarkC is on a distinguished road
Default

There's the available XML parser (see the SeismicXML example from Apple), but for regular HTML, you'll have to write the parsing bits yourself I'm afraid...
MarkC is offline   Reply With Quote
Old 09-10-2009, 07:29 AM   #7 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Dutch will become famous soon enough
Send a message via AIM to Dutch
Default

Quote:
Originally Posted by StevenD View Post
Dutch,

Where in your code does it command that the acquisition of the data runs asynchronously? And is that synonymous with "in another thread"?
/Steve
That method of downloading data is by definition asynchronous. You are executing a connection, and handling each little piece of data as it streams in (yes, on its own thread). The alternative to these three methods would be something like this (not in front of my Mac right now, so this is not tested)..

Code:
-(void)FetchHTMLCode:(NSString *)url{
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    NSString* result=[[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];
    //Do something with result here
    [result release];
}
In this case, when you call [NSData dataWithContentsOfUrl...], the synchronous download takes place. Before result can be determined, the download must finish.

Last edited by Dutch; 09-10-2009 at 07:35 AM.
Dutch is offline   Reply With Quote
Old 09-10-2009, 07:35 AM   #8 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Dutch will become famous soon enough
Send a message via AIM to Dutch
Default

Unfortunately, I am unsure of how to handle international letters - I haven't ventured into that venue quite yet... Check out this link..It lists the various NSStringEncoding Types. You might need to experiment a bit.

Mac Dev Center: NSString Class Reference
Dutch is offline   Reply With Quote
Old 09-10-2009, 03:15 PM   #9 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 21
Alex1987 is on a distinguished road
Default

Well by now I've tried various things but to no avail.

What I did was basically to convert the NSData to a byte array and then I parsed it to go the relevant info and put it in a new byte array. Then I tried to convert the new byte array to a UTF8 string - I got nil

Next I tried to convert the new byte array back to NSData and convert the NSData to NSString - again I got nil.

Here is my code
Code:
-(NSString *)UTF8StringFromData:(NSData *)theData{
	Byte *arr = [theData bytes];
	NSUInteger begin1 = [self findIndexOf:@"<li>" bArr:arr size:[theData length]]+4;
	NSUInteger end1 = [self findIndexOf:@"</li></ol>" bArr:arr size:[theData length]];
	Byte *arr1 = (Byte *)malloc(sizeof(Byte)*((end1-begin1+1)));
	NSLog(@"%d %d",begin1, end1);
	int j = 0;
	for (int i = begin1; i < end1; i++){
		arr1[j] = arr[i];
		NSLog(@"%c",arr1[j]); 
		j++;
	}
	arr1[j]='\0';
	NSData *temp = [NSData dataWithBytes:arr1 length:j];
	NSLog(@"the length: %d", [temp length]);
	
	return [[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding];
What is wrong? I can't believe that converting from NSData to UTF8 string can be so difficult! Someone must have tried it before. Please help

Thanks
Alex1987 is offline   Reply With Quote
Old 09-11-2009, 01:30 PM   #10 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 21
Alex1987 is on a distinguished road
Default

Anyone?
Alex1987 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 334
11 members and 323 guests
givensur, glenn_sayers, guusleijsten, ipodphone, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:30 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0