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
Contributor
Posts: 43
Registered: ‎11-17-2010
My Device: Not Specified
Accepted Solution

How to post messages on a website

Hey Everyone,

 

I was wondering if there is a way to post messages on website using an app I am creating. I know it has to do with API's - but I was wondering how the code structure for such a task would be. I have a text input and a button that should post the message within the text input onto a website. Is there anyway I can make the app automatically post on a page if I provide it with a URL?

 

Thanks in advance!

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

Re: How to post messages on a website

[ Edited ]

hey Jimmy,

 

this code should get you going. it gets a user input from a single line textfield and submit its it as a POST to a webpage that you supply it as a URLRequest and then set the loader up with the data and it submit and once it receives a confirmation that the data was submitted it notifies you. hers the code:

 

 

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	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;
		
		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.rabcore.com/workshop/receivePost.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.showMeData = 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.";
			/*
			 * remove listeners when done
			*/
			loader.removeEventListener(Event.COMPLETE, updateStatus);
		}
	}
}

 

 

 

let me know if you need any explanations. good luck!


Edit: Added comments to source code

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
Contributor
Posts: 43
Registered: ‎11-17-2010
My Device: Not Specified

Re: How to post messages on a website

Thank you so much J Rab., I'm going to give it a shot first thing tomorrow morning.

 

Your help is always appreciated!

 

Developer
Posts: 192
Registered: ‎09-23-2009
My Device: 9860/PlayBook/Dev Alpha
My Carrier: Plus GSM

Re: How to post messages on a website

Have you tried to create data service proxy from FlashBuilder? If your web server operates on json or xml, you will be able to create set of objects for it to use it withou manual post or get.

Found my post helpfull? Like it! It solves your trouble? Help others and accept it as solution. Search before asking.
My apps: Secret Cod3s | Should my tablet be banned?
--
Jakub Nietrzeba
PGS Software S.A.
Your reliable partner in Poland - IT Outsourcing - BlackBerry, iPhone, Web and Standalone applications
Contributor
Posts: 43
Registered: ‎11-17-2010
My Device: Not Specified

Re: How to post messages on a website

Thanks again J Rab. - this gave me a perfect idea of how to get it done and it worked for me. Your comments also helped big time.

 

Gluth I havn't looked into creating a data service proxy yet, but I think that is going to be my next step. Thank you for the heads up!

New Contributor
Posts: 6
Registered: ‎02-25-2011
My Device: Not Specified

Re: How to post messages on a website

Guys! I've been searching for a lead like this. PLEASE HELP. What if after I post the "message" on the website, I want to get back some data/information based on what I sent.

(Baby steps). Can you guys please post some code or email me what you've done. Any help is appreciated! Thanks
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: How to post messages on a website

How are you posting the message to the website?  From within your AIR app?