Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to call Web service from android studio?
Posted
Updated 10-Jun-19 1:07am

Have tried searching this in google[^] prior to post the question here?

Check this article
Calling ASP.NET Webservice (ASMX) from an Android Application, the Simplest Way[^]

Following article may also be useful-
How to call asp.net web service in android[^]

Hope, it helps :)
 
Share this answer
 
class MainActivity : AppCompatActivity() {

    private val MainURL ="http://192.168.1.125/SendMessageService.asmx"
    private val SOAP_ACTION = "http://tempuri.org/InsertMessage"
    private val METHOD_NAME = "InsertMessage"
    private val NAMESPACE = "http://tempuri.org/"




    private val TAG = "TRAINING"
    private val TAG1 = "TRAINING1"
    private val TAG2 = "TRAINING2"
    private val TAG3 = "TRAINING3"
    private val TAG4 = "TRAINING4"
    private val TAG5 = "TRAINING5"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn01.setOnClickListener{
            val myRequest = myAsyncTask()
            myRequest.execute()
        }


    }



    private inner class myAsyncTask : AsyncTask<Void, Void, Void>() {


        override fun doInBackground(vararg params: Void): Void? {


            val URL = MainURL
            System.setProperty("http.keepAlive", "false");
            //for linear parameter
            val request = SoapObject(NAMESPACE, METHOD_NAME)
            request.addProperty("SMS", "hi man"); // adding method property here serially
            request.addProperty("Sender", "444"); // adding method property here serially
            request.addProperty("MessageDateTime", "2010-05-24T18:13:00"); // adding method property here serially

            val envelope = SoapSerializationEnvelope(SoapEnvelope.VER11)
            envelope.implicitTypes = true
            envelope.setOutputSoapObject(request)
            envelope.dotNet = true

            val httpTransport = HttpTransportSE(URL)
            httpTransport.debug = true

            try {

                httpTransport.call(SOAP_ACTION, envelope)
            } catch (e: HttpResponseException) {
                // TODO Auto-generated catch block
                Log.e(TAG1, e.message)
                e.printStackTrace()
            } catch (e: IOException) {
                // TODO Auto-generated catch block
                Log.e(TAG2, e.message)
                e.printStackTrace()
            } catch (e: XmlPullParserException) {
                // TODO Auto-generated catch block
                Log.e(TAG3, e.message)
                e.printStackTrace()
            }
            //send request

            var result: Any? = null
            try {
                result = envelope.response as Any
                Log.i(TAG4, result.toString()) // see output in the console
            } catch (e: SoapFault) {
                // TODO Auto-generated catch block
                Log.e(TAG5, e.message)
                e.printStackTrace()
            }

            return null
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900