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

Adobe AIR Development

Reply
Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition

HTML output in application

Alright, learning time for all.


I'm curious about how the URLRequest and URLLoader functions work in the Playbook/QNX environment? can I get text from a web page (rendered in plain HTML) the same way as I can with a SWF etc??


example:

var stResponse:URLRequest = new URLRequest('http://tncr.ws/example.php');
(...)
addChild(stRequest); stText.htmlText = evt.target.data;


Text from example.php: "That URL has already been shortened!"


....secondly, can I format the text to the screen in any way that doesn't require it to be in a text field?
CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: HTML output in application

It sounds more like you are not returning HTML but a message from the server.  I would suggest the reply from your server be XML, then on return, parse the XML (with XML class) and then display the information "message" to the user in a Label, TextInput or AlertDialog.

 

With the Label class, you can format the font, size, color, bold, etc.

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: HTML output in application

[ Edited ]

hey tensioncore,

 

you have the right idea but need the URLLoader object as well. here is a code showing you how to post data from your application to your website. on your website you would have it listen for the variable "myLongURL" and do what you want with it. after that you would have the website print out a response to the page. After the page is completely done loading your URLLoader object receives the data and dispatches the Event.COMPLETE event and you would have a listener function respond to it when it does. Then you would parse the data to do what it wants. From my experience if you want to show some sort of text on screen you will have to use some variation of the TextField object.

 

here is the code:

 

 

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	import flash.text.TextFieldAutoSize;
	
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.text.Label;
	import qnx.ui.text.TextInput;
	
	[SWF(width="1024",height="600",backgroundColor="#CCCCCC",frameRate="30")]
	public class RandomTests extends Sprite
	{
		private var enterText:TextInput;
		private var enterBtn:LabelButton;
		private var statusText:Label;
		private var loader:URLLoader;
		private var myLoaderData:*;
		
		public function RandomTests()
		{
			enterText = new TextInput();
			enterText.setSize(400, 50);
			
			addChild(enterText);
			
			enterBtn = new LabelButton();
			enterBtn.label = "Click me to post";
			enterBtn.setPosition(410,0);
			enterBtn.setSize(150,50);
			enterBtn.addEventListener(MouseEvent.CLICK, postData);
			
			addChild(enterBtn);
			
			statusText = new Label();
			statusText.autoSize = TextFieldAutoSize.LEFT;
			statusText.setPosition(0,100);
			statusText.format.color = 0xFF0000;
			
			addChild(statusText);
		}
		public function postData(e:MouseEvent):void
		{
			/*
			 * url:String will store the address to your page online
			*/
			var url:String = "http://www.tncr.ws/example.php";
			/*
			 * request:URLRequest is what you send with all your variables
			 * and data to using the loader class
			*/
			var request:URLRequest = new URLRequest(url);
			/*
			 * variables:URLVariables will store all the variables you
			 * want to submit to the webserver
			*/
			var variables:URLVariables = new URLVariables();
			/*
			 * for every variable you want to send use the 
			 * variables.[variable_name] = "string" format
			*/
			variables.myLongURL = enterText.text;
			/*
			 * add the variables to the request 
			*/
			request.data = variables;
			/*
			 * set the method to POST if you are submitting a form
			 * type that does not have a URL query string
			 * set the GET if you want the variables to show up
			 * up as a url query string (e.g. page.php?showMeData=something)
			*/
			request.method = URLRequestMethod.POST;
			
			/*
			 * set up the URLLoader object that will send the request to
			 * the server
			*/
			loader = new URLLoader();
			/*
			 * set up a listener to check when the request has been sent and 
			 * received by the webserver using the Event.COMPLETE event
			*/
			loader.addEventListener(Event.COMPLETE, updateStatus);
			/*
			 * finally send the request to the webserver
			*/
			loader.load(request);
			
			
		}
		public function updateStatus(e:Event):void
		{
			statusText.text = "Data has been submitted successfully.";
			
			myLoaderData = e.target.data;
			
			trace(myLoaderData);
			
			/*
			 * further do what you need to do with the data received
			 * from the server
			*/
			
			/*
			 * remove listeners when done
			*/
			loader.removeEventListener(Event.COMPLETE, updateStatus);
		}
	}
}

 

 

now you can modify the type of response your server sends back. It can be all text, all variables, and all binary data. it depends on what you set for the URLLoader's dataFormat property. You can find more information about here in the ASDocs for the object:

 

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLLoader.html#dataFormat

 

hope that helps! good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition

Re: HTML output in application

here's what I got to work in some form:

here's what I got to work in some form:

 

 

 

var stResponse:URLRequest = new URLRequest('http://www.google.com');
var stField:TextField = new TextField();
stField.width = stage.stageWidth/2;
stField.height = stage.stageHeight/2;
stField.x = stField.y = stage.stageHeight/2;
stField.wordWrap = true;
stField.border = true;

var stLoader:URLLoader = new URLLoader();
stLoader.addEventListener(IOErrorEvent.IO_ERROR, loaderErrorHandler);
stLoader.addEventListener(ProgressEvent.PROGRESS, loaderProgressHandler);
stLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
stLoader.load(stResponse);
addChild(stField);
			
function loaderErrorHandler(evt:IOErrorEvent):void{
	stField.htmlText = 'Could not fetch Content, please check your internet connection and try again';
			}

function loaderProgressHandler(evt:ProgressEvent):void{
var bLoaded:Number = evt.target.bytesLoaded;
var bTotal:Number = evt.target.bytesTotal;
trace(bLoaded + ' / ' + bTotal + ' has loaded');
	}

function loaderCompleteHandler(evt:Event):void{
	stField.htmlText = evt.target.data;
	}

 My only problem Is for now I will need to work on my DNS servers to handle local requests because My servers are in the same network in which I'm developing this app on. LOL

 

CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: HTML output in application

hey,

 

now this is thoery but can you connect to your in house webserver just through an ip address instead of a domain ? such as http://192.168.1.134 (private IP from router)

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition

Re: HTML output in application

Yeah I guess I could do that while I'm in Dev and when I finish the code I can change it to the proper address....however I have about 20 websites on my server (I do web hosting).... guess I could put my BB API onto another port temporarily...I'll post my results.
CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting