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

0

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


Introduction

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=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
   "xmlns:ns1=\"http://schemas.xmlsoap.org/soap/http\" " +
   "xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" " +
   "xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" " +
   "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
   "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" >\n" +
   "<SOAP-ENV:Body>\n" +

 /** Open Body **/
   "<params>\n" +
   "<username>"+"Abhay Anand"+"</username>\n" +
   "<email>"+"anand.abhay1910@gmail.com"+"</email>\n" +
   "</params>\n" +
 /** Close Body **/

   "</SOAP-ENV:Body></SOAP-ENV:Envelope>";

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")
       .post(body)
       .addHeader("Content-Type", "application/xml")
       .addHeader("Authorization", "Your Token")
       .addHeader("cache-control", "no-cache")
       .build();


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

    @Override
    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 = 
            DocumentBuilderFactory.newInstance().newDocumentBuilder();
      final Document parse = newDocumentBuilder.parse(new
             ByteArrayInputStream(response.getBytes()));
      
      String dataString = parse.getElementsByTagName
             ("Your TAG NAME").item(0).getTextContent();
}