You are not currently logged in
Log In | Register

XML API Sample Code (Java for Android)

Tagged with code sample, java, java for android, XML
by Jonny Carter
in Code Samples

03-Nov-2014 00:00

Here is an example function for calling the XML API with Java for Android

JAVA for Android
Calling XML API
// account credentials
String un = "[USERID]";
String pwd = "[PASSWORD]";

//message parameters
String msgContent = "message text body";
String msisdn = "07123456789";

//instantiate HttpClient and HttpPost objects 
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(""); 

String responseStr ="";

// construct the XML input as a String entity and set this as the httpost entity, declaring the content type as text/xml 
// with UTF-8 encoding

try {
    StringEntity input = new StringEntity( 
        "<campaign>" +
        "<header uid='"+un+"' pwd='"+pwd+"' action='SEND_MSISDN'/>" +
        "<body>" +  
        "<msg>"+msgContent+"</msg>" +     
        "<msisdn>"+msisdn+"</msisdn>" +   
        "</body>" +

catch (UnsupportedEncodingException e) {
    // log exception

// Making the POST request and retrieving the response

try {
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();

    // extract the response as a String
    responseStr = EntityUtils.toString(entity);

catch (ClientProtocolException e) {
    //Log exception

catch (IOException e) {
    // Log exception

// output response