Android visits WebService and displays the mobile phone number of the home location

Source: Internet
Author: User

Send XML

Use the URL encapsulation path to open an httpurlconnection

Set the request method, content-type, and Content-Length.

The XML file's Content-Type is: text/XML; charset = UTF-8

Use httpurlconnection to obtain output stream output data


WebService

WebService is an API published on the network. It can be called by sending XML, and the results returned by WebService are also XML data.

WebService has no language restrictions. You only need to send XML data and receive XML data.

The http://www.webxml.com.cn website provides some WebService services that we can call

Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx? OP = getmobilecodeinfo

The content is as follows:

HTTP 1.2

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>

HTTP GET

The following are examples of http get requests and responses. The displayed placeholder must be replaced with the actual value.

GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string HTTP/1.1Host: webservice.webxml.com.cn
HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><string xmlns="http://WebXml.com.cn/">string</string>

HTTP POST

The following are examples of http post requests and responses. The displayed placeholder must be replaced with the actual value.

POST /WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1Host: webservice.webxml.com.cnContent-Type: application/x-www-form-urlencodedContent-Length: lengthmobileCode=string&userID=string
HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length<?xml version="1.0" encoding="utf-8"?><string xmlns="http://WebXml.com.cn/">string</string>

The specific instance and Code are as follows:

Interface display:

XML file sent to WebService: Send. xml(Placed in the SRC path)

<?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>$number</mobileCode>      <userID></userID>    </getMobileCodeInfo>  </soap12:Body></soap12:Envelope>

Layout file man. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <edittext Android: id = "@ + ID/phoneet" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: inputtype = "phone"> <requestfocus/> </edittext> <button Android: onclick = "onclick" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = ""/> <textview Android: Id = "@ + ID/locationtv" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textsize = "25sp"/> </linearlayout>

Function list file androidmanifest. xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.itheima.webservice"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <uses-permission android:name="android.permission.INTERNET"/>      <instrumentation        android:name="android.test.InstrumentationTestRunner"         android:targetPackage="com.itheima.webservice"        />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >    <uses-library android:name="android.test.runner" />        <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>    </application></manifest>

Mainactivity:

Package COM. itheima. webService; import COM. itheima. webService. service. numberservice; 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 phoneet; private textview locationtv; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); phoneet = (edittext) findviewbyid (R. id. phoneet); locationtv = (textview) findviewbyid (R. id. locationtv);} public void onclick (view) {try {numberservice service = new numberservice (); string num = phoneet. gettext (). tostring (); string Loacation = service. getlocation (Num); locationtv. settext (Loacation);} catch (exception e) {e. printstacktrace (); toast. maketext (getapplicationcontext (), "check this number", 1 ). show ();}}}


Numberservice
:

Package COM. itheima. webService. service; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. URL; import Org. xmlpull. v1.xmlpullparser; import android. util. XML; import COM. itheima. webService. util. streamutil; public class numberservice {Public String getlocation (string number) throws exception {// read the locally prepared file and replace the original placeholder inputstream in = numberservice with the entered number. class. getclassloader (). getre Sourceasstream ("send. XML "); byte [] DATA = streamutil. load (in); string content = new string (data); content = content. replace ("$ number", number); // create a connection object, set the request header, set URL url = new URL ("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx") according to the requirements provided by the WebService server "); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5000); Conn. setrequestproperty ("host", "webservi Ce.webxml.com.cn "); Conn. setrequestproperty ("Content-Type", "application/soap + XML; charset = UTF-8"); Conn. setrequestproperty ("Content-Length", content. getbytes (). length + ""); Conn. setrequestmethod ("Post"); // output data Conn. setdooutput (true); Conn. getoutputstream (). write (content. getbytes (); // get the data returned by the server, parse the XML, and get the result xmlpullparser parser = xml. newpullparser (); parser. setinput (Conn. getinputstream (), "UTF-8 "); For (INT type = parser. geteventtype (); type! = Xmlpullparser. end_document; type = parser. next () if (type = xmlpullparser. start_tag & parser. getname (). equals ("getmobilecodeinforesult") {return parser. nexttext () ;}return "this number is not found ";}}

Streamutil:

package com.itheima.webservice.util;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class StreamUtil {   public static byte[] load(InputStream in ) throws IOException{   ByteArrayOutputStream baos = new ByteArrayOutputStream();   byte b [] = new byte[1024];   int len = -1;   while((len=in.read(b))!=-1){   baos.write(b,0,len);   }   baos.close();   return baos.toByteArray();   }}

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.