Introduction
This tutorial shows how to use the playground web service interface to send a SMS from a java client. The tutorial is based on the send SMS Parlay X web service, and illustrates how to create a java client invoking a web service. As a prerequisite we assume that you are familiar with the following technologies:
- HTML
- XML, SOAP and Web Services
- Eclipse - as an environment for Java and web service development
- Java, inclusive basic HTTP/URL handling
- SMS messaging
Follow the steps below to create a client which invokes the SendSms web service.
Place the cursor over this icon to expand screenshots from Eclipse.
- Download and install the Eclipse IDE (Web Tools Platform).
- Create a new project by selecting File | New | Java Project from the menu bar. This initiates the "New Java Project" wizard.
- Give the project a name, and choose your preferences on Java version and project organization. In this tutorial, we named the project "Send SMS Demo". The default values are used for all other selections.
- Push "Finish" to continue and close the wizard. The project is created, and the next steps describe how to start adding the required content to your project.
- Next, you have to create stubs for accessing the web services APIs. The process of creating stubs is described in a separate tutorial - Web services client tutorial - Note that if you create your client based on this tutorial the generated classes will have slightly different names than what is used in the sample code below. Alternatively you can download and store locally the file, playground.jar
- Add the library to your project. To do this, use the right mouse button on your project, and select "Properties", then click "Java Build Path" and "Add External JAR's" button, to select the JAR files. Click the OK button, when you are done.
- In addition to playground.jar you should add the following libraries to your project:
- activation.jar
- mail.jar
- axis.jar - the axis binary download also includes the following required libraries:
- commons-discovery-0.2.jar
- commons-logging-1.0.4.jar
- wsdl4j-1.5.1.jar
- jaxrpc.jar
- saaj.jar
- Add a new Java class to the project. Right click your project, and select "New -> Class" from the menu, to start the "New Java Class" wizard. Name the Java class, in this example "MySendSMSDemo" and check for automatically generating the main method. Click "Finish" and the java class is generated.
- Finally, cut & paste from the source code below into your java class file
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.apache.axis.types.URI;
import org.apache.axis.types.URI.MalformedURIException;
import com.telenor.playground.toolbox.send_sms.*;
import com.telenor.playground.toolbox.send_sms.ParlayXSendSMS_0_1V0_WS;
import com.telenor.playground.toolbox.send_sms.ParlayXSendSMS_0_1V0_WSLocator;
import com.telenor.playground.toolbox.send_sms.PolicyException;
public class MySendSMSDemo {
public MySendSMSDemo(String _username,String _pwd)
{
try {
/**
* Get the SMS service
*/
ParlayXSendSMS_0_1V0_WS sendSMSService = new ParlayXSendSMS_0_1V0_WSLocator();
String smsServiceEndpoint = sendSMSService.getSendSmsPortAddress();
SendSms sendSms = sendSMSService.getSendSmsPort(new URL(smsServiceEndpoint));
/**
* Prepare the parameters needed for sending SMS (
* C = Country code
* N = Phone Number
*/
URI[] addr = new URI[1];
addr[0] = new URI("tel:CCNNNNNNNN");
String msg = "Playground Test - Hello World!";
/**
* Add the necessary authentication data
*/
org.apache.axis.client.Stub stub = (org.apache.axis.client.Stub) sendSms;
stub.setUsername(_username);
stub.setPassword(_pwd);
/**
* Send The SMS
*/
System.out.println("Send SMS response: "+sendSms.sendSms(addr,null,null,msg,null));
} catch (Exception e){
// Handle your error situations here
e.printStackTrace();
}
}
/**
* Use your main method to initiate an instance of the class
* and perform the logic placed in the constructor.
*
* Constructor arguments (username and password) are received
* in your welcome to the lab email
*
*/
public static void main(String[] args) {
MySendSMSDemo mysendsmsdemo = new MySendSMSDemo("username","password");
}
}
|


