A complete Web Service communication should have two parts: the construction of the server and the construction of the client. The construction of the server is cumbersome and can be realized with the help of MyEclipse8.5 with built-in Tomcat.
In fact, there are many established servers on the Internet, which provide developed methods on the server side for external use. For example, many methods are disclosed on the http://www.webxml.com.cn server, such as weather conditions, Flight information, mobile phone attribution, etc., the client only needs to call these methods and related parameters, and can call these methods programmatically, without the need to create a server by itself.
An example is written below:
New main layout file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et" android:hint="Please enter a city" android:selectAllOnFocus="true"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/search_but" android:text="Get weather information"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:textSize="24sp"/> </LinearLayout>Here is the main program class:
package xiao.fuyan.testapp; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; /** * Created by xiao on 2017/2/3. */ public class WebServiceActivity extends Activity { private Button search_but; private TextView textView; private EditText et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.webservice_xml); et = (EditText) findViewById(R.id.et); search_but = (Button) findViewById(R.id.search_but); search_but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String city = et.getText().toString(); getWeather(city, " "); } }); } //define the namespace private static final String NAMESPACE = "http://WebXml.com.cn/"; //Define the URL that requests the WSDL document private static final String URL = "http://www.webxml.com.cn/WebServices/WeatherWS.asmx"; //define the calling method private static final String METHOD_NAME = "getWeather"; //Define namespace + call method name private static final String SOAP_ACTION = "http://WebXml.com.cn/getWeather"; private SoapObject detail; public void getWeather(String city, String id){ try { textView = (TextView) findViewById(R.id.text); //Instantiate the SoapObject object SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); //add parameters rpc.addProperty("thecityname", city); rpc.addProperty("id", id); //Instantiate the SoapSerializationEnvelope object SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //Encapsulate the input SoapObject object rpc envelope.bodyOut = rpc; //The server to be accessed is the .net server, otherwise set to false envelope.dotNet = true; //Set the SoapObject object to output envelope.setOutputSoapObject(rpc); //Specify the WSDL address HttpTransportSE ht = new HttpTransportSE(URL); // use debug ht.debug = true; //call web service ht.call(SOAP_ACTION, envelope); //Get the return information detail = (SoapObject) envelope.getResponse(); textView.setText(detail.toString()); return; }catch (Exception e){ e.printStackTrace (); } } }