Friday, February 05, 2010

Okay, so here is a quick example of using the FedEx WSDL to build a simple tracking application. To get the FedEx WSDL you just go over to their developer website at http://www.fedex.com/us/developer. You will need to sign up to get a key and password to use their service. When you do sign up give it a couple of days for the key and password to be activated. If it takes longer than two days for the key and password to come online, just give the FedEx number a call.

So download the WSDL and add it to your web services on the services tab of NetBeans. Start a new Java Project and drag the track service from the services tab into the main method in your project.

You should now see some code that looks like this:



package javaapplication34;

/**
*
* @author Me
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

try {
com.fedex.ws.track.v4.TrackRequest trackRequest = null;
com.fedex.ws.track.v4.TrackService service = new com.fedex.ws.track.v4.TrackService();
com.fedex.ws.track.v4.TrackPortType port = service.getTrackServicePort();
// TODO process result here
com.fedex.ws.track.v4.TrackReply result = port.track(trackRequest);
System.out.println("Result = " + result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}


We are going to change that code to look like this:



package javaapplication34;

import com.fedex.ws.track.v4.ClientDetail;
import com.fedex.ws.track.v4.Notification;
import com.fedex.ws.track.v4.NotificationSeverityType;
import com.fedex.ws.track.v4.TrackDetail;
import com.fedex.ws.track.v4.TrackIdentifierType;
import com.fedex.ws.track.v4.TrackPackageIdentifier;
import com.fedex.ws.track.v4.TrackRequest;
import com.fedex.ws.track.v4.TransactionDetail;
import com.fedex.ws.track.v4.VersionId;
import com.fedex.ws.track.v4.WebAuthenticationCredential;
import com.fedex.ws.track.v4.WebAuthenticationDetail;
import com.fedex.ws.track.v4.TrackService;
import com.fedex.ws.track.v4.TrackPortType;
import com.fedex.ws.track.v4.TrackReply;

/**
*
* @author me
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

try {
TrackRequest trackRequest = new TrackRequest();
VersionId verId = new VersionId();
verId.setServiceId("trck");
verId.setMajor(4);
verId.setIntermediate(0);
verId.setMinor(0);
//The WSDL does not say that this is required but it is.
trackRequest.setVersion(verId);

WebAuthenticationDetail auth = new WebAuthenticationDetail();
WebAuthenticationCredential cred = new WebAuthenticationCredential();
//This is the key.
cred.setKey("XXXX");
//This is the password
cred.setPassword("XXXX");
auth.setUserCredential(cred);

ClientDetail detail = new ClientDetail();
//This is the test account number
detail.setAccountNumber("XXXX");
//This is the test meter number
detail.setMeterNumber("XXXX");

TransactionDetail trans_detail = new TransactionDetail();
trans_detail.setCustomerTransactionId("Tracking with Java");

TrackPackageIdentifier packId = new TrackPackageIdentifier();
packId.setType(TrackIdentifierType.TRACKING_NUMBER_OR_DOORTAG);
//This is some tracking number that you already have.
packId.setValue("XXXX");

trackRequest.setWebAuthenticationDetail(auth);
trackRequest.setClientDetail(detail);
trackRequest.setTransactionDetail(trans_detail);
trackRequest.setPackageIdentifier(packId);
trackRequest.setIncludeDetailedScans(Boolean.TRUE);

TrackService service = new TrackService();
TrackPortType port = service.getTrackServicePort();

TrackReply result = port.track(trackRequest);
if (result.getHighestSeverity() != NotificationSeverityType.FAILURE && result.getHighestSeverity() != NotificationSeverityType.ERROR) {
System.out.println("----RESULTS----");
if (!result.getTrackDetails().isEmpty()) {
for (TrackDetail d : result.getTrackDetails()) {
System.out.println("Tracking Number " + d.getTrackingNumber());
System.out.println("Tracking Description " + d.getStatusDescription());
}
}
} else {
for (Notification n : result.getNotifications()) {
System.err.println(n.getMessage());
}
}
} catch (Exception ex) {
ex.printStackTrace();
}

}
}


Ta-da! Simple example of the FedEx tracking Web-Service in action. If your account has not been enabled yet you will receive an "Authentication Failed" message. Otherwise you should start seeing some detail about the package. I suggest that you give the TrackDetail class a quick glance since that will be where most of the information you need to access will be. Remember to read those pesky annotations in the WSDL for more information and the PDF on the FedEx developer's website.
Post a Comment