Extending the basic Java application using Bango’s web services.
Following on from my first post, my second objective was to develop a way to make it so that my application will only work if the user has a valid Bango subscription with some time remaining on it. This started with quite a lot of research into how I was going to do it.
The first obvious problem was how to get the application to find out if the user has a valid and active subscription. I played around with a few different techniques and approaches, the one that appeared to be the most successful was to have the application connect to a web server which would then send a web service request to Bango. The web server then returns some data to the phone (Illustrated right).
So the first stage was to develop a script on the web server to get the correct information to the device. The information I needed to get to the device was really only the time remaining on the subscription. I did however actually decide to send a few extra pieces of data to the phone too though for some further ideas I had.
To get the subscription status, we have to go through three major stages:
- Get the users Bango user ID
- Get the users active subscriptions on a Bango number
- Get the time remaining on the subscription
To do this I used PHP as my server-side scripting language, and this is what I came up with.
<?php
$username = “myusername”; // Bango web services username
$password = “mypassword”; // Bango web services password
$bango = “000111222333”; // Bango number with subscriptions
$script = “http://domain.com/userid.php”; // URL of this script
if(isset($_GET[‘b’])){
$client = new SoapClient(“https://webservices.bango.com/subscriptions/service.asmx?wsdl”);
$SubscriptionID = $client->GetSubscriptionID(array(“request” =>
array( “username” => $username,
“password” => $password,
“bango” => $bango,
“userId” => $_GET[‘u’])
));
$SubscriptionInfo = $client->GetSubscriptionInfo(array(“request” =>
array( “username” => $username,
“password” => $password,
“subscriptionId” => $SubscriptionID->GetSubscriptionIDResult->subscriptionId)
));
// Redirect to same script, but only with the variable we need.
// We then scrape the variable in the app.
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: ” . $script . “?time=” . $SubscriptionInfo->GetSubscriptionInfoResult->accessMinutesRemaining);
}
?>
I did actually have quite a few problems with PHP and SOAP. There are quite a few ways of using SOAP; I used the PHP SOAP module (http://uk.php.net/manual/en/book.soap.php).
I then setup a Bango number with the identifier function enabled which redirects to the script. I was now set to work on two more things:
- Getting the data into the application
- Using the data to make the application expire
String scripturl = “http://domain.com/userid.php”; // The same url you entered in the php script
String bangoid = “444333222111”; //the bango number with identifier function turned on
HttpConnection conn = null;
String url = “http://bango.net/id/?bango=” + bangoid + “®ister=n”;
try {
while (true) {
conn = (HttpConnection) Connector.open( url );
conn.setRequestProperty( “User-Agent”, “Mozilla/4.0” );
rc = conn.getResponseCode();
if ((rc == HttpConnection.HTTP_TEMP_REDIRECT) ||
(rc == HttpConnection.HTTP_MOVED_TEMP) ||
(rc == HttpConnection.HTTP_MOVED_PERM)) {
// Get the new location and close the connection
url = conn.getHeaderField(“location”);
conn.close();
} else {
String timeremaining = replaceAll(conn.getURL(),scripturl + “?time=”,””);
conn.close();
break;
}
}
}catch( IOException e ){
// handle the error here
}
This also requires a small method called replaceAll, below.
private String replaceAll(String text, String searchString, String replacementString){
StringBuffer sBuffer = new StringBuffer();
int pos = 0;
while((pos = text.indexOf(searchString)) != -1){
sBuffer.append(text.substring(0, pos) + replacementString);
text = text.substring(pos + searchString.length());
}
sBuffer.append(text);
return sBuffer.toString();
}
I then just compared the variable timeremaining against 0. If the number is greater than 0, the application will start. If not we tell the user that the subscription doesn’t exist or has expired.
if(Integer.parseInt(timeremaining) > 0){
// Code if not expired
}else{
// Code if expired
}
What could be easier?
Recent Comments