Mono for Android calls webserver Service

Source: Internet
Author: User
Implementation-simple calculator function:

The program is divided into two parts,

1. websever Service

2. Android Client

---------------------------------------

1. websever Service

(Only icalculator. CS, Calculator. CS are used in the Service class. Other interface classes and methods used to implement this interface are not used in this program)

1-1 icalculator Interface Class

/*************************************** * ****** Class ID: * ** use: implement the calculator interface **. OPERATOR: LCB *** write date: *************************************** */using system; using system. collections. generic; using system. text; using system. servicemodel; namespace ceservice {[servicecontract] public interface icalculator {// <summary> // addition /// </Summary> // <Param name = "num1"> first number </param> /// <Param name = "num2"> second count </param> /// <Param name = "num"> result </param> /// <returns> </returns> [operationcontract] string add (double num1, double num2, ref double num ); /// <summary> /// subtraction /// </Summary> /// <Param name = "num1"> first number </param> /// <Param name = "num2"> second count </param> /// <Param name = "num"> result </param> /// <returns> </returns> [operationcontract] string reduce (double num1, double num2, ref double num ); /// <summary> /// multiplication // </Summary> /// <Param name = "num1"> first number </param> /// <Param name = "num2"> second count </param> /// <Param name = "num"> result </param> /// <returns> </returns> [operationcontract] string Division (double num1, double num2, ref double num ); /// <summary> /// division /// </Summary> /// <Param name = "num1"> first number </param> /// <Param name = "num2"> second count </param> /// <Param name = "num"> result </param> /// <returns> </returns> [operationcontract] string multiplication (double num1, double num2, ref double num );}}

1-2 Implementation Interface Class Calculator

/*************************************** * ****** Class ID: * ** usage: implement the calculator class. ** OPERATOR: LCB *** write date: *************************************** */using system; using system. collections. generic; using system. text; namespace ceservice {public class calculator: icalculator {// <summary> /// addition /// </Summary> /// <Param name = "num1"> first number </param> /// <param name = "num2"> second count </param> /// <Param name = "num"> result </param> /// <returns> </returns>/ // Public String add (double num1, double num2, ref double num) {try {num = num1 + num2; Return "1";} catch (exception ex) {return ex. tostring ();}} /// <summary> /// subtraction /// </Summary> /// <Param name = "num1"> first number </param> /// <Param name = "num2"> second count </param> /// <Param name = "num"> result </param> /// <returns> </returns> Public String reduce (double num1, double num2, ref double num) {try {num = num1-num2; Return "1";} catch (exception ex) {return ex. tostring ();}} /// <summary> /// multiplication // </Summary> /// <Param name = "num1"> first number </param> /// <Param name = "num2"> second count </param> /// <Param name = "num"> result </param> /// <returns> </returns> Public String Division (double num1, double num2, ref double num) {try {num = num1 * num2; Return "1";} catch (exception ex) {return ex. tostring ();}} /// <summary> /// division /// </Summary> /// <Param name = "num1"> first number </param> /// <Param name = "num2"> second count </param> /// <Param name = "num"> result </param> /// <returns> </returns> Public String Multiplication (double num1, double num2, ref double num) {try {num = num1/num2; Return "1";} catch (exception ex) {return ex. tostring ();}}}}

1-3 app. config

<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <! -- <Appsettings> <add key = "connectionstring" value = "Data Source = .; initial catalog = yanshi2010; uid = sa; pwd = sa "/> <add key =" smsport "value =" 3 "/> <add key =" smsbudrate "value =" 19200 "/> <add key =" smsstate "value =" false "/> </appsettings> --> <system. servicemodel> <services> <! -- Platform service ------------- start --> <! -- Service interface implementation class --> <service name = "ceservice. Calculator" behaviorconfiguration = "servicebehavior"> <! -- Service interface --> <endpoint address = "" binding = "basichttpbinding" Contract = "ceservice. icalculator "/> 

Enable the Service (you all know how to write the code, and omit it. Focuses on Android)

----------------------------------------------------------------

----------------------------------------------------------------

2. Android Client

Overall project:

2-1 add web reference (the address is set in your configuration file, for example)

2-2 main. axml

Code:

<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <edittext Android: Id = "@ + ID/txtphoneno" Android: layout_width = "304.0dp" Android: phonenumber = "true" Android: hint = "Enter the first number" Android: layout_height = "wrap_content" Android: layout_margintop = "5px" Android: layout_marginleft = "5px"/> <edittext Android: Id = "@ + ID/txtsms" Android: layout_width = "313.3dp" Android: layout_height = "401_dp" Android: phonenumber = "true" Android: gravity = "TOP" Android: hint = "enter the second number" Android: layout_below = "@ ID/txtphoneno"/> <button Android: id = "@ + ID/btnadd" Android: text = "plus" Android: layout_width = "69dp" Android: layout_height = "wrap_content" Android: layout_below = "@ ID/txtsms"/> <button Android: Id = "@ + ID/btnjian" Android: text = "subtraction" Android: layout_width = "69dp" Android: layout_height = "wrap_content" Android: layout_below = "@ ID/txtsms" Android: layout_torightof = "@ ID/btnadd"/> <button Android: id = "@ + ID/btncheng" Android: text = "" Android: layout_width = "69dp" Android: layout_height = "wrap_content" Android: layout_below = "@ ID/txtsms" Android: layout_torightof = "@ ID/btnjian"/> <button Android: Id = "@ + ID/btnchu" Android: TEXT = "except" Android: layout_width = "69dp" Android: layout_height = "wrap_content" Android: layout_below = "@ ID/txtsms" Android: layout_torightof = "@ ID/btncheng"/> <edittext Android: Id = "@ + ID/txtjieguo" Android: layout_width = "fill_parent" Android: layout_height = "40.7dp" Android: gravity = "TOP" Android: hint = "" Android: layout_below = "@ ID/btnchu"/> </relativelayout>

2-3 activity1.cs

Using system; using Android. APP; using Android. content; using Android. runtime; using Android. views; using Android. widget; using Android. OS; namespace databasedemo {[activity (Label = "databasedemo", mainlauncher = true, icon = "@ drawable/icon")] public class activity1: Activity {edittext tvjg; edittext num1; edittext num2; double num = 0; protected override void oncreate (bundle) {base. oncreate (bundle); // set our view from the "Main" layout resource setcontentview (resource. layout. main); // Add button btnadd = findviewbyid <button> (resource. id. btnadd); // subtract button btnjian = findviewbyid <button> (resource. id. btnjian); // by button btncheng = findviewbyid <button> (resource. id. btncheng); // except button btnchu = findviewbyid <button> (resource. id. btnchu); // The first number num1 = findviewbyid <edittext> (resource.id.txt phoneno); // The second number num2 = findviewbyid <edittext> (resource.id.txt SMS ); // result tvjg = findviewbyid <edittext> (resource.id.txt jieguo); webreference. calculator CA = new webreference. calculator (); btnadd. click + = delegate {double num1s = convert. todouble (num1.text. tostring (); double num2s = convert. todouble (num2.text. tostring (); string flag = Ca. add (num1s, num2s, ref num); tvjg. TEXT = num1s. tostring () + "+" + num2s. tostring () + "=" + num. tostring () ;}; btnjian. click + = delegate {double num1s = convert. todouble (num1.text. tostring (); double num2s = convert. todouble (num2.text. tostring (); string flag = Ca. reduce (num1s, num2s, ref num); tvjg. TEXT = num1s. tostring () + "-" + num2s. tostring () + "=" + num. tostring () ;}; btncheng. click + = delegate {double num1s = convert. todouble (num1.text. tostring (); double num2s = convert. todouble (num2.text. tostring (); string flag = Ca. division (num1s, num2s, ref num); tvjg. TEXT = num1s. tostring () + "*" + num2s. tostring () + "=" + num. tostring () ;}; btnchu. click + = delegate {double num1s = convert. todouble (num1.text. tostring (); double num2s = convert. todouble (num2.text. tostring (); string flag = Ca. multiplication (num1s, num2s, ref num); tvjg. TEXT = num1s. tostring () + "/" + num2s. tostring () + "=" + num. tostring ();};}}}

Code, which has been completed!

Source code download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.