Many Widset/Widget applications are limited by the fact that their APIs does not provide support for much of the functionality that is available to native applications. This could be functions like:
As for now, according to the Web Developers' Library- messaging, media and location are available since WRT 1.1. An easy fix to obtain these functions in Widgets is to use the Mobile Web Server to provide these functions.
This can be achieved in a few steps:
The examples below shows how to get information from MWS
* Widget Example
function queryMWS()
{
// This code uses Prototype, but a standard javascript request will work just as well
new Ajax.Request("http://localhost/hello.py",
{
method: 'get',
asynchronous: 'false',
onSuccess: function(transport, json)
{
return true;
},
onFailure: function()
{
return false;
}
});
}
* Widset Example
void queryMWS()
{
// Prepare the URL.
String URL = "https://MWS_username:MWS_password@MWS_username.mymobilesite.net/hello.py";
// Fetch from the URL.
call(null, "httpLocalhost", "get", ["url" => URL], success, failure);
void success(Object state, Value ret)
{
setBubble(null, "The server successfully returned " + ret);
return;
}
void failure(Object state, String error)
{
setBubble(null, "Local server is not responding properly.");
return;
}
}
Two files is necessary to get python executing on MWS:
*ht.acl
AuthType Basic
AuthName 'Hello'
AuthBasicProvider file
AuthUserFile conf/passwords.txt
AuthGroupFile conf/groups.txt
# If you want to give access to all users
#Require valid-user
# If you want to give access to only yourself or some groups (add all needed groups delimited with space)
Require group admin
# If you want to give access to some users (add all needed users delimited with space)
#Require user quest
AddHandler mod_python .py
PythonHandler hello
PythonDebug On
Options None
Order Deny,Allow
Allow from all
<FilesMatch "\.(pyc)$">
Deny from all
</FilesMatch>
*hello.py
def handler(req):
from mod_python import apache
import httplib
try:
#
# Execute any python code here
#
req.write("Hello World")
req.status = 200
return apache.OK
except Exception, e:
req.status = 404
return apache.OK
Good examples of how to use python modules can be found at the Mobile Python Book and Python section.
* Example of using python to get the current location
def handler(req):
from mod_python import apache
import location
import positioning
req.content_type = 'text/xml'
req.write("<?xml version='1.0' encoding='utf-8'?>")
req.write("<twitnflick>")
req.write("<modules count='" + str(len(positioning.modules())) + "' default='"
+ str(positioning.default_module()) + "'>")
for n in range(0, len(positioning.modules())):
req.write("<module id='" + str(positioning.modules()[n]['id']) + "'>")
req.write("<name>" + positioning.modules()[n]['name'] + "</name>")
req.write("<available>" + str(positioning.modules()[n]['available'])
+ "</available>")
req.write("</module>")
req.write("</modules>")
positioning.set_requestors([{"type":"service","format":"application","data":"test"}])
gpspos = positioning.position()
req.write("<position>")
req.write("<latitude>" + str(gpspos['position']['latitude']) + "</latitude>")
req.write("<longitude>" + str(gpspos['position']['longitude']) + "</longitude>")
req.write("<altitude>" + str(gpspos['position']['altitude']) + "</altitude>")
req.write("<horizontal_accuracy>" + str(gpspos['position']['horizontal_accuracy'])
+ "</horizontal_accuracy>")
req.write("<vertical_accuracy>" + str(gpspos['position']['vertical_accuracy'])
+ "</vertical_accuracy>")
req.write("</position>")
req.write("</twitnflick>")
return apache.OK
No related wiki articles found