Ajax pada Webworks Blackberry

Method get pada JQLite dilakukan dengan cara sebagai berikut :

jQuery.ajax.send("content.html", function(data, status) {
alert(data);
});

Hal ini akan menghasilkan result yang sama dengan cara tradisional:

function startajax(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","content.html",true);
xmlhttp.onreadystatechange=function(event){
if(xmlhttp.readyState==4){
alert(xmlhttp.responseText);
}
};
xmlhttp.send(null);
}
function update(){
if(xmlhttp.readyState==4){
alert(xmlhttp.responseText);
}
}

Hello World


JQLite , sebagai javascript library di Blackberry

Javascript library di Blackberry Webworks dapat menggunakan JQLite, ini adalah sebuah library yang mirip dengan JQuery. Browser blackberry, tidak /belum support JQuery. Library ini dapat di unduh di http://code.google.com/p/jqlite/

Setelah mengekstrak file ini, file – file di impor ke direktori projek kita. Tips: keluarkan semua file dari foldernya, sehingga semua library javascript tidak berada dalam folder. Berdasarkan pengalaman, error berikut ini terjadi karena file library berada dalam folder:

Invalid widget archive – resource name is not valid(extensions/.svn/all-wcprops)

Memang tidak semua fungsional JQuery disupport, akan tetapi library ini sudah cukup lumayan. Fitur-fitur dasar antara lain: show, hide, animate,  dan toggleClass.

Sedangkan cara penggunaan, library ini mirip dengan JQuery. Kita masih bisa menggunakan blok program berikut:


$(document).ready(function(){

..........

})

Selamat mencoba

Blackberry PAP push

PAP push allow you to send push message using Blackberry Internet Service. This service uses XML .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace pap_push
{
class push
{
public push(String param)
{
string pushId = “pushID:” + (new Random()).Next();
HttpWebResponse HttpWRes = null;
HttpWebRequest HttpWReq = null;
string pin = “2100000A”;
string BOUNDARY = “mPsbVQo0a68eIL3OAxnm”;
string msg = param;
string url = “http://localhost:8080/pap”;
HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.Method = (“POST”);
HttpWReq.Accept = “text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2″;
HttpWReq.ContentType = “multipart/related; boundary=” + BOUNDARY + “;             type=application/xml”;
HttpWReq.Headers.Add(“x-Rim-Push-Dest-Port”, “100″);
HttpWReq.Headers.Add(“X-WAP-Application-id”, “/”);
StringBuilder dataToSend = new StringBuilder();
dataToSend.AppendLine(“–” + BOUNDARY);
dataToSend.AppendLine(“Content-Type: application/xml; charset=UTF-8″);
dataToSend.AppendLine(“”);
dataToSend.AppendLine(“<?xml version=\”1.0\”?>”);
dataToSend.AppendLine(“<!DOCTYPE pap PUBLIC \”-//WAPFORUM//DTD PAP 2.0//EN\” “);
dataToSend.AppendLine(“\”http://www.wapforum.org/DTD/pap_2.0.dtd\” “);
dataToSend.AppendLine(“[<?wap-pap-ver supported-versions=\"2.0\"?>]>”);
dataToSend.AppendLine(“<pap>”);
string myPushId = DateTime.Now.ToFileTime().ToString();
dataToSend.AppendLine(“<push-message push-id=\”" + pushId + “\” ppg-notify-requested-to=\”http://localhost:7778\”>”);
dataToSend.AppendLine(“<address address-value=\”WAPPUSH=” + pin + “%3A100/TYPE=USER@rim.net\” />”);
dataToSend.AppendLine(“<quality-of-service delivery-method=\”confirmed\” />”);
dataToSend.AppendLine(“</push-message>”);
dataToSend.AppendLine(“</pap>”);
dataToSend.AppendLine(“–” + BOUNDARY);
dataToSend.AppendLine(“Content-Type: text/plain”);
//dataToSend.AppendLine(“Push-Message-ID: ” + pushId);
dataToSend.AppendLine(“”);
dataToSend.AppendLine(msg);
dataToSend.AppendLine(“–” + BOUNDARY + “–”);
dataToSend.AppendLine(“”);
Stream requestStream = null;
string pushResult = “”;
try { requestStream = HttpWReq.GetRequestStream(); }
catch (Exception ex)
{
pushResult = “Push failed! ” + ex.ToString();
}
byte[] outStr = new ASCIIEncoding().GetBytes(dataToSend.ToString());
requestStream.Write(outStr, 0, outStr.Length);
requestStream.Close();
try
{
HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
}
catch (Exception ex)
{            //push failed
}
if (HttpWRes != null)
{
HttpWRes.Close();
}
}
}
}

Your device should access the rim url (http://www.rim.com) before the application able to listen the push.

Blackberry RIM Push

With Blackberry RIM push, you send push message using html header request instead of XML. It can only used with Blackberry Enterprise Server

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace rim_push
{
class push
{
String _param;
byte[] data = null;
public push(String param)
{
_param = param;
}
public void httpPush(string BESAddress, string BESWebserverListenPort,
string pushTitle, string url, string pushPin)
{
string pushPort = “100″;
string httpURL = “http://”+ BESAddress +”:”+ BESWebserverListenPort +”/push?DESTINATION=”+ pushPin +”&PORT=”+ pushPort +”&REQUESTURI=/”;
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(httpURL);
HttpWReq.Method = (“POST”);
HttpWReq.Headers.Add(“Content-Location”, url);
HttpWReq.Headers.Add(“X-RIM-Push-Title”, pushTitle);
data = StrToByteArray(_param);
HttpWReq.ContentLength = data.Length;
Stream requestStream = HttpWReq.GetRequestStream();
requestStream.Write(data, 0, data.Length);
HttpWebResponse HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
requestStream.Close();
HttpWRes.Close();
}
catch (System.Exception e)
{
}
}
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
}
}

If you use a Blackberry simulator, you need an MDS running to see the result with Blackberry simulator

First add this code as listener to your button:

private void button1_Click(object sender, EventArgs e)
{
push push = new push(textBox1.Text);
push.httpPush(“localhost”, “8080″,  “x”, “localhost:8080″,”2100000A”);
}

make sure you can access localhost:8080 in your computer, thats mean your MDS is running. The default pin number is 2100000A, be sure the number is still the default.

Blackberry push

There are two kinds of blackberry push: RIM push and PAP push. You need MDS server to simulate your push application using Blackberry simulator.
This code is a sample of Blackberry push enabled with Webworks environment:

 

<!DOCTYPE HTML PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”>
<html>
<head>
<h5>sample push application by pujie:abunajiyah.wordpress.com</h5>
<script type=”text/javascript”>
var port = 100;
function subscribe() {
blackberry.push.openPushListener(function(data) {
if (data != null) {
var text = blackberry.utils.blobToString(data.payload);
alert(“You have a push message:” + text );
} else {
alert(“no data from push”);
}
}, port);
alert(“push Listening has just started”);
}
function unsubscribe() {
blackberry.push.closePushListener(port);
alert(“Push listening has stopped”);
}
</script>
<meta name=”viewport” id=”viewport” content=”initial-scale=1.0,user-scalable=no” />
<title>Job Seeker</title>
</head>
<body>
<button id=”btnsubscribe” onclick=”subscribe()”>Subscribe</button>
<button id=”btnunsubscribe” onclick=”unsubscribe()”>Un Subscribe</button>
</body>
</html>

In your config.xml , add these features in your widget permission: blackberry.push, blackberry.system, blackberry.utils

Follow

Get every new post delivered to your Inbox.