Implementation Functions of this app:
Note: http://webservice.webxml.com.cn/webservices/mobilecodews.asmxis the provider of webservicein this document.
Specific usage see: http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx? OP = getmobilecodeinfo
The following are examples of soap 1.2 requests and responses. The displayed placeholder must be replaced with the actual value.
POST /WebServices/MobileCodeWS.asmx HTTP/1.1Host: webservice.webxml.com.cnContent-Type: application/soap+xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getMobileCodeInfo xmlns="http://WebXml.com.cn/"> <mobileCode>string</mobileCode> <userID>string</userID> </getMobileCodeInfo> </soap12:Body></soap12:Envelope>HTTP/1.1 200 OKContent-Type: application/soap+xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"> <getMobileCodeInfoResult>string</getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap12:Body></soap12:Envelope>
Step 1: Create an android project mobileaddressquery and SET network access permissions.
Step 2: design the application UI and use the string value
/RES/values/string. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, mainactivity! </String> <string name = "app_name"> mobile phone number attribution query </string> <string name = "mobile"> mobile phone number </string> <string name = "button "> local query </string> <string name =" error "> query failed </string> </resources>
Layout/RES/layout/Main. xml
<? 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"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/mobile"/> <edittext Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/mobile"/> <button Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/button" Android: onclick = "query"/> <! -- Specify the response method of the button as query --> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: id = "@ + ID/textview"/> </linearlayout>
Step 3: Create sop12.xml In the src directory and copy the Code provided in the URL document, as shown below:
<? XML version = "1.0" encoding = "UTF-8"?> <Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope"> <soap12: body> <getmobilecodeinfo xmlns = "http://WebXml.com.cn/"> <! -- $ Mobile only shows this, after the program calls this XML file, it will replace the mobile phone number entered by the user --> <mobilecode> $ mobile </mobilecode> <userid> </getmobilecodeinfo> </soap12: body> </soap12: envelope>
Step 4: Business Class: CN. roco. Address. addressservice
Note that the access destination address is:
Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
You can get it in the Protocol.
Package CN. roco. address. service; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. URL; import Org. xmlpull. v1.xmlpullparser; import android. util. XML; import CN. roco. utils. streamtools; public class addressservice {/*** is used to obtain the mobile phone number and call WebService * @ Param mobile phone number * @ return * @ throws exception */public static string getaddress (string mobile) throws exception {string soap = reads Oap ();/*** Regular Expression $ is a special symbol in a special regular expression that must be escaped, that is, \ $ mobile * And \ is a special symbol in a string. Therefore, two backslashes are used, that is, "\ {1} quot; */soap = soap. replaceall ("\ $ mobile", mobile); // Replace the mobile phone number entered by the user with $ mobilebyte [] entity = soap in sop12.xml. getbytes (); string Path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; // WebService provides the source httpurlconnection connection = (httpurlconnection) new URL (PATH ). openconnection (); connection. setconnecttimeout (5000 ); Connection. setrequestmethod ("Post"); connection. setrequestproperty ("Content-Type", "application/soap + XML; charset = UTF-8"); connection. setrequestproperty ("Content-Length", String. valueof (entity. length); connection. setdooutput (true); connection. getoutputstream (). write (entity); If (connection. getresponsecode () == 200) {return parsesoap (connection. getinputstream ();} return NULL;}/*** parse the data returned by WebService * <? XML version = "1.0" encoding = "UTF-8"?> <Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope"> <soap12: body> <getmobilecodeinforesponse xmlns = "http://WebXml.com.cn/"> <getmobilecodeinforesult> string </getmobilecodeinforesult> </getmobilecodeinforesponse> </soap12: Body> </soap12: envelope> * @ Param inputstream * @ return * @ THR Ows exception */Private Static string parsesoap (inputstream XML) throws exception {xmlpullparser pullparser = xml. newpullparser (); pullparser. setinput (XML, "UTF-8"); int event = pullparser. geteventtype (); While (event! = Xmlpullparser. end_document) {Switch (event) {Case xmlpullparser. start_tag: If ("getmobilecodeinforesult ". equals (pullparser. getname () {return pullparser. nexttext ();} break;} event = pullparser. next ();} return NULL;}/*** read sop12.xml content * @ return * @ throws exception */Private Static string readsoap () throws exception {inputstream = addressservice. class. getclassloader (). getresourceasstream ("sop12.xml"); byte [] DATA = streamtools. read (inputstream); return new string (data );}}
Step 5: tool class CN. roco. utils. streamtool
Package CN. roco. utils; import Java. io. bytearrayoutputstream; import Java. io. inputstream; public class streamtools {/*** read data from the input stream * @ Param inputstream input stream * @ return binary stream data * @ throws exception */public static byte [] Read (inputstream) throws exception {bytearrayoutputstream outputstream = new bytearrayoutputstream (); byte [] buffer = new byte [1024]; int length = 0; while (length = inputstream. read (Buf Fer ))! =-1) {outputstream. Write (buffer, 0, length);} inputstream. Close (); Return outputstream. tobytearray ();}}
Step 6: CN. roco. Address. mainactivity
package cn.roco.address;import cn.roco.address.service.AddressService;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private EditText mobileText;private TextView textView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mobileText = (EditText) this.findViewById(R.id.mobile);textView = (TextView) this.findViewById(R.id.textView);}public void query(View v) {String mobile=mobileText.getText().toString();try {String address=AddressService.getAddress(mobile);textView.setText(address);} catch (Exception e) {e.printStackTrace();Toast.makeText(getApplicationContext(), R.string.error, 1).show();}}}
Step 7: androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "CN. roco. address "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "/> <! -- Access Internet permissions --> <uses-Permission Android: Name = "android. permission. internet "/> <application Android: icon =" @ drawable/icon "Android: Label =" @ string/app_name "> <activity Android: Name = ". mainactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> <uses-library Android: Name =" android. test. runner "/> </Application> <instrumentation Android: targetpackage =" CN. roco. address "Android: Name =" android. test. instrumentationtestrunner "/> </manifest>
========================================================== ========================================================== ============================
Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!
Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng
========================================================== ========================================================== ============================