Android- Sending XML OR SOAP REST API Request Using OKHTTP Tutorial


In this tutorial, we will learn how to send HTTP Post & Get XML Request or SOAP web service using OkHttp Library in Android Application.


Simple Object Access Protocol (SOAP) is a web services that allows provider to send envelope for sending message. It is based on Extensible Markup Language(XML) for message Android Soap Reset APIformat, and usually relies on other Application Layer protocols, most notably Hypertext Transfer Protocol (HTTP) and Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.

In the Previous tutorial, I have explained, how to send HTTP POST and GET request over Internet using OKHTTP Library. In this tutorial, we will learn how to send HTTP POST and GET XML request means how to call SOAP Rest API.

Add Internet Permission

To Send POST and GET request to server, means to create connection between server and Android Application, we need to give Internet permission to Android Application. Open your Manifest file and below code:

<uses-permission android:name="android.permission.INTERNET" />

Add OkHttp Library

We need to add OkHttp libary in the dependency. Open build.gradle(Module:app) file and add following code:

dependencies {
    compile 'com.squareup.okhttp3:okhttp:3.4.1'

SOAP Request Data

SOAP allows us to send envelope. We will request API as XMl format, hence we need to create data string as XML format. We will use Media Type application/XMl.

String soap_string = 
   "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
   "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"\" " +
   "xmlns:ns1=\"\" " +
   "xmlns:soap=\"\" " +
   "xmlns:wsdl=\"\" " +
   "xmlns:xsd=\"\" " +
   "xmlns:xsi=\"\" >\n" +
   "<SOAP-ENV:Body>\n" +

 /** Open Body **/
   "<params>\n" +
   "<username>"+"Abhay Anand"+"</username>\n" +
   "<email>"+""+"</email>\n" +
   "</params>\n" +
 /** Close Body **/


Use OkHttp

Now, use OkHttp to send XML request to server and get XMl response from server. We explained OKHttp in previous tutorial.

public static final MediaType SOAP_MEDIA_TYPE = MediaType.parse("application/xml");
final OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(SOAP_MEDIA_TYPE, soap_string);

final Request request = new Request.Builder()
       .url("Your URL")
       .addHeader("Content-Type", "application/xml")
       .addHeader("Authorization", "Your Token")
       .addHeader("cache-control", "no-cache")

client.newCall(request).enqueue(new Callback() {
    public void onFailure(Call call, IOException e) {
          String mMessage = e.getMessage().toString();
          Log.w("failure Response", mMessage);

    public void onResponse(Call call, Response response) throws IOException {

         String mMessage = response.body().string();

          //code = response.code();
          getResponse(mMessage, response);

Get Response In Xml Format

We need to create a new method getResponse(). In this method, we will retrieve data from XML using DocumentBuilder Object.

public void getResponse(String response, Response mainRes){

      DocumentBuilder newDocumentBuilder = 
      final Document parse = newDocumentBuilder.parse(new
      String dataString = parse.getElementsByTagName
             ("Your TAG NAME").item(0).getTextContent();

  • John Ortiz

    Tremendous! Thanks for sharing this code! It had solved my problem with SOAP requests!

  • Yatin Kharbe

    i am getting Unsupported Media Type as response … Response Code – 415

  • Oğuzhan Yapıcı

    Thank you for tutorial. I’m using this code in another class, it’s working but i cant get the data from there to my main_activity