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.

Don't Miss

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();
}

4 COMMENTS

  1. 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

564FansLike

Recent Posts

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.

Related Articles

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.
WP2Social Auto Publish Powered By : XYZScripts.com