Tutorial: Using the Zipwhip API in Java
June 19, 2011 3 Comments
I’m new to the Zipwhip API so I figured I would share my experience sending out my first text message using the Zipwhip system. To follow along with this article you will need to get your hands on the Zipwhip API as a JAR file. It’s available here for download:
Zipwhip API Jar File (170KB) ZipwhipAPI.jar
Release Date: 6/18/2011
First, open up Eclipse and make a new Java project.
Call the project ZipwhipAPIExample. (BTW, you can zoom in on the screenshots by clicking them.)
You can leave the Source tab alone, however you will want to add the ZipwhipAPI Jar to the Libraries tab.
Make sure you download the ZipwhipAPI Jar from the link at the start of this article. Then, add it to the Libraries list by choosing “Add External JARs” and finding the file on your local computer. Make sure to add the Log4J Jar file as well since the ZipwhipAPI is dependant upon it. You will get compile errors if you don’t have Log4J somewhere in the build path.
You should now have a new project in your Package Explorer. You are ready to go.
Make a new Java class file by right-clicking the src folder and choosing “Class”.
Call your class “Example” after you’ve entered a package name of something like “com.yourcompany.zipwhipapi”. Make sure to also create a public static void main() method so you have an entry point for your Java app.
After you hit “Finish” you should see something similar to the following generated code.
Now, we’ll help you out with the code you should place into the Example.java file in order to send off your first text message using the Zipwhip API. Make sure you set your own mobile number and password in the code. If you don’t know what your password is, or you have never signed up for Zipwhip, you need to do it via your carrier website if your carrier supports Zipwhip. Zipwhip has contracts with most major U.S. carriers so please visit your carrier site or call your carrier to find out how to gain access.
Download source code below Example.java
1: package com.yourcompany.zipwhipapi;
2:
3: import com.zipwhip.api.DefaultZipwhipClient;
4: import com.zipwhip.api.HttpConnection;
5: import com.zipwhip.api.ZipwhipClient;
6:
7: public class Example {
8:
9: public static void main(String[] args) {
10:
11: HttpConnection connection = new HttpConnection();
12:
13: String mobileNumber = "3135551212";
14: String password = "mypassword";
15:
16: try {
17: connection.requestLogin(mobileNumber, password);
18: ZipwhipClient zipwhipClient = new DefaultZipwhipClient(connection);
19:
20: // Ok, go ahead and send your message
21: zipwhipClient.sendMessage("3134440002", "This is our first test SMS message.");
22:
23: System.out.println("Done sending message");
24:
25: } catch (Exception e) {
26: // TODO Auto-generated catch block
27: e.printStackTrace();
28: }
29: }
30: }
This is what your Eclipse window should look like when all of the code is done and ready to go.
That’s it. Have fun sending off text messages via Zipwhip’s spectacularly simple API. If you want to get fancy, try to use some of the other methods in the API that let you create contacts, create groups, receive messages back, let you setup a URL to be posted to when an SMS comes in, etc.
To learn how to receive text messages via the Zipwhip API you can read my next posting.