Call us now on 01772 217800

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("http://textgoto.com/api/xmlapi.aspx"); 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>" + "</campaign>", HTTP.UTF_8); input.setContentType("text/xml"); httpPost.setEntity(input); } catch (UnsupportedEncodingException e) { // log exception e.printStackTrace(); } // 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 e.printStackTrace(); } catch (IOException e) { // Log exception e.printStackTrace(); } // output response Toast.makeText(getBaseContext(),responseStr,TOAST.LENGTH_LONG).show();