Thursday, 21 July 2016

Consuming SOAP Webservice using HttpURLConnection in Android

Consuming SOAP Webservice in Android using HttpUrlConnection.

Create a class like below. Please make sure to call this function in background Thread/AsyncTask/Service.

ServiceHandler.java
====================
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
 * Created by pratap.kesaboyina on 07-05-2016.
 */
public class ServiceHandler {
    /*
        requestUrl= endpoint
        methodName = namespace+ServiceMethodName
        soapxml= soap xml string
      */
    public static ServiceResponse soapWebServiceRequest(String requestUrl, String methodName, String soapBody) {

        ServiceResponse serviceResponse = new ServiceResponse();
        URL oURL = null;
        InputStream inputStream = null;
        try {
            oURL = new URL(requestUrl);
            HttpURLConnection con = (HttpURLConnection) oURL.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-type", "text/xml; charset=utf-8");
            con.setRequestProperty("SOAPAction", methodName);

            OutputStream reqStream = con.getOutputStream();
            reqStream.write(soapBody.getBytes());

            // if 200 , then proceed
            int statusCode = con.getResponseCode();

            if (statusCode == 200) {

                inputStream = con.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                String line = "";
                String result = "";

                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }

            /* Close Stream */
                if (null != inputStream) {
                    inputStream.close();
                }

                serviceResponse.setIsSuccessOrFail(true);
                serviceResponse.setXmlResponse(result);
            } else {
                serviceResponse.setIsSuccessOrFail(false);
                serviceResponse.setXmlResponse("Error in Connection");
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
            serviceResponse.setIsSuccessOrFail(false);
            serviceResponse.setXmlResponse("Error in Connection");
        } catch (ProtocolException e) {
            e.printStackTrace();
            serviceResponse.setIsSuccessOrFail(false);
            serviceResponse.setXmlResponse("Error in Connection");
        } catch (IOException e) {
            e.printStackTrace();
            serviceResponse.setIsSuccessOrFail(false);
            serviceResponse.setXmlResponse("Error in Connection");
        }


        return serviceResponse;


    }


}



ServiceResponse.java
==================
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
**
 * Created by pratap.kesaboyina on 26-08-2015.
 */
public class ServiceResponse {

    // is request failed/sucess
    private boolean isSuccessOrFail;

    // if request success then xmlResponse else contains error msg
    private String xmlResponse;


    public ServiceResponse() {
    }


    public ServiceResponse(boolean isSuccessOrFail, String xmlResponse) {
        this.isSuccessOrFail = isSuccessOrFail;
        this.xmlResponse = xmlResponse;
    }


    public void setIsSuccessOrFail(boolean isSuccessOrFail) {
        this.isSuccessOrFail = isSuccessOrFail;
    }

    public boolean isSuccessOrFail() {
        return isSuccessOrFail;
    }


    public String getXmlResponse() {
        return xmlResponse;
    }

    public void setXmlResponse(String xmlResponse) {
        this.xmlResponse = xmlResponse;
    }


}




RecyclerView using Kotlin

A Simple example to create a RecyclerView using Kotlin Language. build.gradle [under app folder] ============ apply plugin: &#...