Air - What are the ways to recieve data from the client? - actionscript-3

I am making an Air application and I want it to receive data input from the user, like the user fills in a text field, and send them to the developer's app.
I am not asking for code , I am just looking for ways to do this.
Thanks

Here is a usefull link to Adobe's Flex in a Week videotraining:
Flex in a Week, Day3. See section "Sending remote data".
I suggest you should watch the whole series if you got spare time. It will help you learn more about flex.
More Info.
One way that this trainning uses is HTTPService.
Within the <fx:Declarations> block create a new HTTPService like so:
<fx:Declarations>
<s:HTTPService id="sendToServerService"
url=" YOUR_SERVER_URL_HERE "
method="POST"/>
<fx:Declarations>
Note: With the HTTPService class, you can communicate with PHP pages, ColdFusion pages, JavaServer Pages (JSP), Java servlets, Ruby on Rails, and Microsoft Active Server Pages (ASP).
An example of your server url is http://www.example.com/remoteData/addUserDatat.cfm using ColdFusion.
Within the Script block create a function that gets the data from the input:
protected function onSendBtn(event:MouseEvent):void
{
var data:String = textInput1.text;
vehicleService.send(data);
}
In your Application file create a button and a textInput control:
<s:Button id="sendBtn" label="Send" click="onSendBtn(event)"/>
<s:TextInput id="textInput1"/>

Related

Accessing REST api from Windows CE

I have one Windows Handheld device application which has the requirement of accessing a REST API. The REST API gives me JSON output which I am going to handle via Newton.JSON. Now to achieve modular structure I want to have the communication to the REST API be handled via a different module altogether something like a Class Library. But unfortunately it seems that it is not possible to do so via a class library(or maybe possible). So my question is what is the best alternative to do so?
Please note that I don't want to include those connectivity operations in my front end application project. And I am using .Net framework 3.5 & Windows Mobile SDK 6.0
Thanks in advance
Pseudo class library code:
public function void startQuery() //starts a thread that does the JSON query
//inside thread on query result use OnDone() delegate
private delegate void OnDone(string dateTimeString);
//In main GUI code add a reference to the class lib and init a new object then add an event handler to the OnDone delegate of the class lib
JSONClassLib myJson=new JSONClassLib();
...
myJson.OnDone+=new EventHandler(myEventHandler);
void myEventHandler(sender this, objext o){
//will be called when query is done
}
//you need to use Control.Invoke if you want to update the GUI from myEventHandler
//to start a query use something like this from your class lib
myJson.doQuery(string);
If you add your existing code we may help with creating a class lib and async code
Now I got my answer. Sorry I did a mistake while selecting the project type. I selected "Windows Form Class Library" project instead of "Smart Device Class Library" project. Now that I have selected the right one it is working fine for me.
BTW thanks for those responses.
Cheers

Mvvmcross, how to access a ViewModel from my android project

I have an Android app which uses a SupportActionBar that contains a bunch of tabs. These tabs each have a fragment that in turn are connected to a ViewModel in my core project. This works great and when i start the app they are all initialized right away and setup correctly.
What i would like to do is to call on a method on one of these ViewModels from my main activity that contains all the tabs and fragments.
I read in another post that in WP you could cast the DataContext to the ViewModel but that might not work in Android. I haven't been able to do this, maybe because my DataContext is not the currently displayed ViewModel but the MainViewModel connected to my main activity. Or maybe it's not supposed to be done that way, i'm not sure.
I'm trying to do this:
var test = (MessagesViewModel)this.DataContext;
test.GetViewDataFromApi();
To update the data in the view when i press the tab. I can't use the Init function for this for example since the ViewModel isn't recreated everytime i show the view.
Are you trying to update some data in the tab's fragment when tab is selected?
If that's the case, one way to do it is to
1) handle the tab selection event to get the current tab(maybe using TabListener),
2) get the fragment (MvxFagment) in the selected tab
3) get the (IMvxViewModel) view-model from the fragment
4) call the method you need to update data on the view-model
I assume you are using a MvxFragment (https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Droid.Fragging/Fragments/MvxFragment.cs?source=cc) so you can access the view-model from the MvxFragment's ViewModel property.

Use MessageDialog/MessageBox with Portable Class Library and MVVM Light

I´m developing an App that will be available for Windows Phone 8 and the Windows Store. To reduce redundancy I´m using a Portable Class Library (PCL) and on top of that I'm trying to apply the MVVM pattern with the help of the MVVM Light PCL Toolkit. The ViewModels are placed in the PCL and are bound directly in the XAML of the Apps pages.
When the data is received without an error, everything works fine. But I don´t know how to get the exceptions/error message back to the App when errors do happen.
Inside the Windows Store App errors will show as a MessageDialog while the Wp8 App will use the MessageBox class. Obviously the PCL isn´t aware of any of these classes. What I´m not getting is how to know if a ViewModel ran into an error, and how to get the message inside the App. Is this even possible when the ViewModels are bound inside the XAML?
The code in the ViewModel (inside the PCL) looks like this:
DataService.Authenticate((token, error) =>
{
if (error != null)
{
// This is, obviously, not going to work.
MessageBox.Show(error.Message);
return;
}
Token = token;
});
So I have to save the error somehow and let the App itself know the error has occurred, and then call the matching way of showing the error to the user.
Currently I´m thinking of something like defining an Error-property inside the BaseViewModel and fill it when errors in the ViewModel occur. Then, in the CodeBehind of the pages, make them aware of the current ViewModel and bind a PropertyChanged-event to this Error-property. But I was not able to implement it yet, so I don't know if this is even the right way to go.
Do I have to step down from the idea to bind the ViewModels inside the XAML, and do I instead have to initialize them inside the pages Codebehind?
Your instinct is correct, but there are more than a few ways of going about this.
First and foremost, you can use Mvvm's Messaging library, which will allow your ViewModel to send messages directly to your View. Your View can then handle it in any way it wishes, including but not limited to using a MessageDialog.
Secondly, you can also create a Function or Action (likely the former) in your ViewModelLocator for ShowMessageDialog. This Function will likely take a string and return a Task. Then, after you initialize your ViewModelLocator initially, you can inject your ShowMessageDialog code. Your ViewModels can then use whatever platform's MessageDialogs that they please.
Ex:
Note: This code uses the BCL Async libraries that are accessible in Nuget. They work in the PCL just fine.
ViewModelLocator:
public static Func<string, Task> ShowMessageDialog { get; set; }
App.xaml.cs:
ViewModelLocator.ShowMessageDialog = (message) =>
{
// For Windows Phone
return TaskFactory.StartNew(() => MessageBox.Show(message));
// For Windows 8
MessageDialog md = new MessageDialog(message);
return md.ShowAsync().AsTask();
};
ViewModel:
await ViewModelLocator.ShowMessageDialog("This is my message.");
Secondary Note: The md.ShowAsync().AsTask(); must be run on the UI Thread. This means that you will have to invoke it via the dispatcher in the case that you are running it in a task asynchronously. This is possible using a similar method of injecting the use of the app's CoreDispatcher via the RunAsync method.
This means that you can, on any platform (Windows 8 and Windows Phone shown above), inject whatever Message Dialog system you want and use it in your PCL.
I would say that it is much easier to do the first method I suggested, as that is what it is there for, but the Function method version is definitely helpful at times.

Externalize app version info

I want my app's preloader to display version/build info that I have in an XML file. Obviously, the preloader can't pick it up because it waits for XML file to load. Is there another approach that I can use to externalize my version/build info and ensure it's in my app early?
Any tips are helpful and very appreciated.
Thanks!
I typically use compiler constants for this data, as it's available at build time, and can be easily baked into the app.
-define+=CONFIG::VERSION "1.0.1-a"
private static const version:String = CONFIG::VERSION;
<s:Label text="{version}" />

Bug? LoaderInfo getLoaderInfoByDefinition security sandbox violation in AIR

This question is specific to Adobe AIR ActionScript/Flash applications. I've spend quite some time researching this and kept finding discussions about either the Flash Player security model when running in the browser, where issues with SWF loading can be mitigated in various ways (loading from the same domain, using a crossdomain.xml file, etc.) or for AIR HTML applications, where the JavaScript security model is discussed with it's per-frame sandboxes and the sandbox bridge approach. My problem is different, I believe.
First some code. To demonstrate, I created a very simple Flex application (LoaderInfoTest.mxml):
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="onCreationComplete(event)">
<mx:Script>
<![CDATA[
import flash.display.LoaderInfo;
import flash.system.ApplicationDomain;
import flash.utils.getQualifiedClassName;
import mx.events.FlexEvent;
public function onCreationComplete(event:FlexEvent):void
{
// the following line of code throws an exception
var info:LoaderInfo = LoaderInfo.getLoaderInfoByDefinition(this);
}
]]>
</mx:Script>
</mx:WindowedApplication>
... and an application.xml descriptor file (LoaderInfoTest-app.xml):
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<application xmlns="http://ns.adobe.com/air/application/1.5.2">
<id>LoaderInfoTest</id>
<filename>LoaderInfoTest</filename>
<name>LoaderInfoTest</name>
<version>v1</version>
<initialWindow>
<content>LoaderInfoTest.swf</content>
</initialWindow>
</application>
I build this using Flash Builder 4 Beta, but I presume the issue remains the same when using the SDK command line tools. When executing this either from within Flash Builder or from the command line via:
> adl LoaderInfoTest-app.xml .
I get a popup with this exception:
Security sandbox violation: caller app:/LoaderInfoTest.swf cannot access LoaderInfo.applicationDomain owned by app:/LoaderInfoTest.swf. at flash.display::LoaderInfo$/getLoaderInfoByDefinition() ...
I don't understand why the SWF cannot access the LoaderInfo.applicationDomain property (presumably a protected or internal property) owned by itself. The documentation for LoaderInfo.getLoaderInfoByDefinition states that it's possible for a SecurityError to be thrown, if "[t]he caller is not running in the local trusted sandbox". Unless I really have a gross misunderstanding of the AIR security model, a local SWF runs with full trust (application sandbox). So, why is this not working? Is it a bug in the AIR runtime?.
I should note that in a different scenario, when running this code as a pure Flash (not AIR) application in the Flash player, it does work.
The best answer would be some sort of configuration or setting I can change to make this work (maybe in the application descriptor?) ... or pointing out where I am making a mistake. The second-best answer would be a definite source of explanation of why this will never work.
1st Edit - Before anyone points it out: I know that inside the onCreationComplete method, this.loaderInfo gives me access to the current LoaderInfo instance. My example here is the simplest I could come up with to demonstrate the "problem." The context in which I want to use LoaderInfo.getLoaderInfoByDefinition(this) is not a DisplayObject instance.
2nd Edit - I am considering even accepting a link to where I can post a bug to Adobe AIR's issue tracker as an answer. The public Flex issue tracker doesn't count, because this is not a Flex problem.
3rd Edit - It is apparent that there are differences between the "local trusted sandbox" and the "AIR application sandbox," but (some of) these differences seem non-sensical and I now consider them a bug, at least in the context of this question and especially because it works in Flash Player.
The documentation is correct that getLoaderInfoByDefinition is available only to content in the localTrusted sandbox. Although AIR application content has many privileges, it is not in localTrusted and therefore cannot use the API.
It's certainly a reasonable request, however, to add access for application content.
As a workaround, you can use this API (and Sampler APIs) in AIR by loading another SWF in localTrusted sandbox. To do this, you need to add the file to one of the trusted lists, and load the file with a file:// URL (not app:/). There are then a number of ways for the localTrusted and application content to communicated.
You can file bugs against AIR (as well as make feature requests) at www.adobe.com/go/wish
I suspect the security error may be a red herring. It doesn't look like there should be one here.
This issue is also showing up in a regular flash application. Basically, I have a helper class called UrlInfo.
It's constructor looks like this
import flash.display.LoaderInfo;
public class UrlInfo
{
private var _loaderInfo:LoaderInfo;
public function UrlInfo():void
{
_loaderInfo = LoaderInfo.getLoaderInfoByDefinition(this);
}
}
In a fla file, I have this:
import my.namespace.UrlInfo;
var ui:UrlInfo = new UrlInfo();
I get the same error:
SecurityError: Error #2119: Security sandbox violation: caller file **SAMEFILE.swf** cannot access LoaderInfo.applicationDomain owned by **SAMEFILE.swf**.
at flash.display::LoaderInfo$/getLoaderInfoByDefinition()
at com.honda.ttd.content.as3.util::UrlInfo()
at urlinfo_fla::MainTimeline/frame1()
Launching the .html that calls the swf does not trigger this.
Launching the .swf does trigger this.
I know that I can fix it by going to the flash settings and adding the location of the swf into the Flash Security Settings Tab.
For instance, if the SAMEFILE.swf is on my Desktop, I can add C:/ to the list of trusted locations.
I AGREE THAT THIS IS ODD because the file is accessing itself, yet it is violating some security. I would like to know if there is any fix for this or if this is actually expected behavior.