Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
FlorianCourtial
Posts: 6
Registered: ‎09-20-2012
My Device: Curve 8520
My Carrier: Orange
Accepted Solution

How to get html code of a web page ?

Hi,

 

Fisrtly i'm a french student so sorry for my bad level in english... I would like to do an application that recover information on a web site, for this I want to recover a html page into a string, after this, i cut the string in order to recover specific information.

 

My question is : How can I  get html code of a web page into a String ?

 

In Java i can, but with Blackberry JDK I don't know,

 

Waiting for your answer.

 

Courtial Florian

Please use plain text.
Developer
simon_hain
Posts: 15,846
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: How to get html code of a web page ?

welcome to the support forum.

you download the html with a standard http get operation. see the httpdemo for a good sample.

basically you need
http://www.blackberry.com/developers/docs/7.1.0api/javax/microedition/io/HttpConnection.html (also contains a sample)
and
http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/io/IOUtilities.html#streamToBy... (as the simplest method to read the inputstream)

as the BB has some difficulties with different network access types you may also need http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/io/transport/ConnectionFactory...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
FlorianCourtial
Posts: 6
Registered: ‎09-20-2012
My Device: Curve 8520
My Carrier: Orange

Re: How to get html code of a web page ?

Thank you, I will try soon :smileyhappy:

Please use plain text.
Developer
Ebscer
Posts: 843
Registered: ‎08-31-2009
My Device: 9530, 9630, 9800, 8530, 9900, 9810, 9930, PlayBook, Dev Alpha
My Carrier: Verizon

Re: How to get html code of a web page ?

I use the following method where the paramter passed in is your url.

 

private static String GetPageText(String text)
{
	ConnectionFactory factory = new ConnectionFactory();
	ConnectionDescriptor conDescriptor = factory.getConnection(text);
	try
	{
		StreamConnection s = (StreamConnection) conDescriptor.getConnection();
		InputStream input = s.openInputStream();

		byte[] data = new byte[256];
		int len = 0;
		StringBuffer raw = new StringBuffer();
		while ( -1 != (len = input.read(data)) )
		{
			raw.append(new String(data, 0, len));
		}

		text = raw.toString();
		input.close();
		s.close();
	}
	catch (Exception e) {return "";}
	return text;
}

 


Read my thoughts on BlackBerry Development at news.ebscer.com
Please use plain text.
Developer
simon_hain
Posts: 15,846
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: How to get html code of a web page ?

oh, and be aware of the event thread:
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

networking and other blocking operations have to be done on a separate thread.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
FlorianCourtial
Posts: 6
Registered: ‎09-20-2012
My Device: Curve 8520
My Carrier: Orange

Re: How to get html code of a web page ?

I try, but I have a problem.

This is my code :

public void loadCodeSource (String url) {
		
		String text = null;
		ConnectionFactory factory = new ConnectionFactory();
		ConnectionDescriptor conDescriptor = factory.getConnection(url);
		
		try
		{
			StreamConnection s = (StreamConnection) conDescriptor.getConnection();
			InputStream input = s.openInputStream();
			
			byte[] dataTest = IOUtilities.streamToBytes(input);
			text = new String(dataTest);
			
			//In order to see the result
			UiApplication.getUiApplication().pushGlobalScreen(new Dialog(Dialog.OK,text.substring(0, 100),Dialog.OK,null,Dialog.GLOBAL_STATUS),500,UiEngine.GLOBAL_QUEUE);
			input.close();
			s.close();
			this.setCodeSource(text);
		}
		catch (Exception e) {
			UiApplication.getUiApplication().pushGlobalScreen(new Dialog(Dialog.OK,"No network...",Dialog.OK,null,Dialog.GLOBAL_STATUS),500,UiEngine.GLOBAL_QUEUE);
		}
	}

 And my string text is empty, i think the connection fails...

Please use plain text.
Developer
simon_hain
Posts: 15,846
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: How to get html code of a web page ?

i would suggest to use HttpConnection, as you access a http source.
you should also check the response code of the connection as you could encounter different error messages from the server.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
FlorianCourtial
Posts: 6
Registered: ‎09-20-2012
My Device: Curve 8520
My Carrier: Orange

Re: How to get html code of a web page ?

I find the problem, i had a bad URL  :Boat:

Thank you very much for your help,

I close the subject...

Please use plain text.
New Contributor
FlorianCourtial
Posts: 6
Registered: ‎09-20-2012
My Device: Curve 8520
My Carrier: Orange

Re: How to get html code of a web page ?

For my test I used StreamConnection but I will use HTTPConnection because I have to connect to a website in order to get information.

 

Thank you.

Please use plain text.
New Contributor
FlorianCourtial
Posts: 6
Registered: ‎09-20-2012
My Device: Curve 8520
My Carrier: Orange

Re: How to get html code of a web page ?

I have a new problem, i d'ont open a new topic because my problem concerns HTML code. When I recover the code on the website, I recover nothing.

Indeed I try to recover HTML code on a dynamic page. This is a page generated by a JavaServer. (The page is a .jsp).

When the HTML code is recover, the JavaServer has not yet created the page.

So how can I recover HTML code ?

 

Waiting for your answer,

 

Courtial Florian

Please use plain text.