Soap Services in Android Studio

Overview

SOAP stands for Simple Object Access Protocol. It's an XML based protocol for accessing webservices over HTTP. It could be used across all applications. As it is web based it is platform independent.

Code

 android:usesCleartextTraffic="true"
 <uses-permission android:name="android.permission.INTERNET" />
 class ConstantString
{
companion object
{
const val SOAP_ACTION : String = "http://www.w3schools.com/webservices/"
const val NAME_SPACE : String = "http://www.w3schools.com/webservices/"
const val URL : String = "http://www.w3schools.com/webservices/tempconvert.asmx"
const val F_TO_C_METHOD_NAME : String = "FahrenheitToCelsius"
const val C_TO_F_METHOD_NAME : String = "CelsiusToFahrenheit"
const val F_TO_C_SOAP_ACTION : String = SOAP_ACTION + F_TO_C_METHOD_NAME
const val C_TO_F_SOAP_ACTION : String = SOAP_ACTION + C_TO_F_METHOD_NAME
}
}
 class WebServiceCall
{
companion object
{
var TAG : String = WebServiceCall::class.java.simpleName
fun callWebServiceThreadSoapPrimitive(
strURL : String , strSoapAction : String , request : SoapObject
) : String
{
try
{
val envelope : SoapSerializationEnvelope =
SoapSerializationEnvelope(SoapEnvelope.VER11)
envelope.dotNet = true
envelope.setOutputSoapObject(request)
val httpTransPortSE = HttpTransportSE(strURL)
httpTransPortSE.debug = true
httpTransPortSE.call(strSoapAction , envelope)
val response : SoapPrimitive = envelope.response as SoapPrimitive
val result = StringBuffer(response.toString())
return result.toString()
} catch (e : Exception)
{
e.printStackTrace()
return ""
}
}
}
}
 class SoapTask(
val context : Context ,
val soapAction : String ,
val methodName : String ,
val paramsName : String
) : AsyncTask<String , Void , String>()
{
override fun doInBackground(vararg params : String?) : String
{
val soapObject : SoapObject = SoapObject(ConstantString.NAME_SPACE , methodName)
soapObject.addProperty(paramsName , params)
return WebServiceCall.callWebServiceThreadSoapPrimitive(
ConstantString.URL , soapAction , soapObject
)
}

override fun onPostExecute(result : String?)
{
super.onPostExecute(result)
if (result == null)
{
Toast.makeText(context , "Cannot Get Result!!" , Toast.LENGTH_LONG).show()
} else
{
(context as MainActivity).callBackDataFromAsyncTask(result)
}
}
}
 class MainActivity : AppCompatActivity() , View.OnClickListener
{
private var convertStyle = 0
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnToCelsius.setOnClickListener(this)
btnToFahrenheit.setOnClickListener(this)
}

override fun onClick(view : View?)
{
if (view?.id == R.id.btnToCelsius)
{
invokeAsyncTask(
ConstantString.F_TO_C_SOAP_ACTION , ConstantString.F_TO_C_METHOD_NAME , "Celsius"
)
convertStyle = 0
} else
{
invokeAsyncTask(
ConstantString.C_TO_F_SOAP_ACTION , ConstantString.C_TO_F_METHOD_NAME , "Fahrenheit"
)
convertStyle = 1
}
}

private fun invokeAsyncTask(soapAction : String , methodName : String , convertParams : String)
{
SoapTask(this , soapAction , methodName , convertParams).execute(
edtInput.text.toString().trim()
)
}

fun callBackDataFromAsyncTask(result : String)
{
val resultTemperature : Double = result.toDouble()
if (convertStyle == 0)
{
tvResult.text = String.format("%.2f" , resultTemperature) + " degree Celsius"
} else
{
tvResult.text = String.format("%.2f" , resultTemperature) + " degree Fahrenheit"
}
}
}
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/edtInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="100dp"
android:text=""
android:textAlignment="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:id="@+id/llButtonContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="horizontal"
android:weightSum="1"
app:layout_constraintEnd_toEndOf="@+id/edtInput"
app:layout_constraintStart_toStartOf="@+id/edtInput"
app:layout_constraintTop_toBottomOf="@+id/edtInput">

<Button
android:id="@+id/btnToCelsius"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="0.5"
android:text="To Celsius"
android:textAllCaps="false" />

<Button
android:id="@+id/btnToFahrenheit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_weight="0.5"
android:text="To Fahrenheit"
android:textAllCaps="false" />

</LinearLayout>


<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/llButtonContainer" />

</androidx.constraintlayout.widget.ConstraintLayout>

Comments

Popular posts from this blog

Architecture Components in Android

DataBinding in Android

SSLSocketFactory in Android