The Webservice method is called by android ., Androidwebservice

Source: Internet
Author: User

The Webservice method is called by android ., Androidwebservice

1. Background:

The author wants to implement android webservice calls. However, the Internet is full of Irrelevant articles, and the results will not solve the problem, but will only upset people, therefore, the author decided to share with everyone the implementation of webservice calls that can be used by android, so that people who need the same service can avoid detours in the future.

The source code is written in android studio and can be downloaded and viewed on github: https://github.com/jhscpang/testwebswervice.

 

2. Specific implementation:

The focus of this Article is on how android calls webservice instead of webservice, so here we will test webservice with a large number of computing calls uploaded over the Internet. This webservice address: http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx? Wsdl.

You can see the following page when accessing this URL in a browser:

The circled Part 1 in the figure indicates that the soap version is 12, and the circled Part 2 shows the namespace address, which can be used later in the code.

The circled section in the figure shows the name of the method to be called. The instructions in this section indicate the input parameters, return values, and other information, which will be used later in the code.

 

The following describes how to request webservice. The Code is as follows:

/*** Phone number segment attribution query ** @ param phoneSec phone number segment */public String getRemoteInfo (String phoneSec) throws Exception {String WSDL_URI = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx? WSDL "; // wsdl uri String namespace =" http://WebXml.com.cn/"; // namespace String methodName =" getMobileCodeInfo "; // The name of the method to be called SoapObject request = new SoapObject (namespace, methodName); // you can specify the parameters mobileCode and userId request to be passed in to call the WebService interface. addProperty ("mobileCode", phoneSec); request. addProperty ("userId", ""); // create a SoapSerializationEnvelope object and specify the soap version number (as shown in the wsdl earlier). SoapSerializationEnvelope envelope = new SoapSerializationEnvelope. VER12); envelope. bodyOut = request; // set bodyOut envelope because the request is sent. dotNet = true; // because it is. the webservice developed by. net, so set it to true HttpTransportSE httpTransportSE = new HttpTransportSE (WSDL_URI); httpTransportSE. call (null, envelope); // call // obtain the returned data SoapObject object = (SoapObject) envelope. bodyIn; // get the returned result = object. getProperty (0 ). toString (); Log. d ("debug", result); return result ;}

Because webservice is a networked operation, you cannot access webservice In the UI thread. To facilitate feedback to the UI thread, use the AsyncTask thread. The Code is as follows:

Class QueryAddressTask extends AsyncTask <String, Integer, String >{@ Override protected String doInBackground (String... params) {// query the mobile phone number (segment) information */try {result = getRemoteInfo (params [0]);} catch (Exception e) {e. printStackTrace ();} // return the result to the onPostExecute method return result;} @ Override // This method can change UI protected void onPostExecute (String result) in the main thread) {// display the results returned by WebService in the TextView resultView. setText (result );}}

Then, set the method for using this function in the main thread. The Code is as follows:

Private EditText phoneSecEditText; private TextView resultView; private Button queryButton; private String result; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); phoneSecEditText = (EditText) findViewById (R. id. phone_sec); resultView = (TextView) findViewById (R. id. result_text); queryButton = (Button) findView ById (R. id. query_btn); queryButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// mobile phone number (segment) String phoneSec = phoneSecEditText. getText (). toString (). trim (); // you can easily determine whether the entered mobile phone number (segment) is legal if ("". equals (phoneSec) | phoneSec. length () <7) {// error message: phoneSecEditText. setError ("the phone number (segment) You entered is incorrect! "); PhoneSecEditText. requestFocus (); // clears the resultView of the query result. setText (""); return;} // start the background asynchronous thread to connect to webService, and change UI QueryAddressTask queryAddressTask = new QueryAddressTask () in the main thread Based on the returned results (); // start the background task queryAddressTask.exe cute (phoneSec );}});}

The layout file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: paddingTop = "5dip" android: paddingLeft = "5dip" android: paddingRight = "5dip"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "mobile phone number (segment ): "/> <EditText android: id =" @ + id/phone_sec "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: inputType =" textPhonetic "android: singleLine = "true" android: hint = "Example: 1398547"/> <Button android: id = "@ + id/query_btn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "right" android: text = "query"/> <TextView android: id = "@ + id/result_text" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center_horizontal | center_vertical"/> </LinearLayout>

The AndroidManifest file is as follows:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.jhsc.testwebservice" >    <uses-permission android:name="android.permission.INTERNET" />    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme" >        <activity android:name=".MainActivity" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

The running effect is as follows:

Related Article

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.