Account  |   Register  |   Pricing  |   Asterisk  |   HTTP API   |   Code  |   Batch Client   |   Networks  |   Status  |   About  |   Contact

Number Portability Lookup - Sample Code for Carrier Query: Java

Java Sample Code:

The Java CarrierQueryDemo class below provides an example of querying a mobile network carrier using the NumberPortabilityLookup service.

The class uses a java.net.URL connection to interface with the NumberPortabilityLookup carrier query service over its HTTP API. The response is read one line at a time using a BufferedReader wrapped around the InputStream from the URL object, and may be extended to read multiple MSISDNs (if you perform a batch query) by repeating the in.readLine() call for each number until dataLine is found to contain "ENDBATCH".

The code provided here should be easy to adapt for use in any standalone Java application, Java Applet, Java Servlet (or JSP page), or in a bean context - anywhere you need to add carrier lookup functionality to your code!

Other Code Samples & Asterisk Carrier Lookup Examples

To find code samples for other languages, or number portability lookup integration examples for the Asterisk PBX, click here.

Java CarrierQueryDemo Class:


import java.net.*;
import java.io.*;
import java.util.*;

public class CarrierQueryDemo
{
	public static void main(String args[]) throws Exception
	{
		String user="my-username";
		String pass="my-password";
		String msisdn="447785123456";
		String carrier=null;

		URL url = new URL("http://api.numberportabilitylookup.com/npl?user="+user+"&pass="+pass+"&msisdn="+msisdn);

		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
		String queryOK = in.readLine();

		if (queryOK.equals("QUERYOK"))
		{
			String dataLine = in.readLine();
			StringTokenizer st = new StringTokenizer(dataLine," ");
			msisdn =(String)st.nextElement();
			carrier=(String)st.nextElement();
		}
		System.out.println("MSISDN "+msisdn+" has carrier "+carrier);

		in.close(); in=null; url=null;
	}
}



 
NumberPortabilityLookup.com™ & © 2021 SES IP Holdings Ltd - All Rights Reserved. Service operated by Wizard Island Software LLC.