`
bk_lin
  • 浏览: 321342 次
社区版块
存档分类
最新评论

android 之访问WebService显示手机号码归属地

阅读更多

发送XML

通过URL封装路径打开一个HttpURLConnection

设置请求方式,Content-Type和Content-Length

XML文件的Content-Type为:text/xml;charset=UTF-8

使用HttpURLConnection获取输出流输出数据

 

WebService

WebService是发布在网络上的API,可以通过发送XML调用,WebService返回结果也是XML数据

WebService没有语言限制,只要可以发送XML数据和接收XML数据即可

http://www.webxml.com.cn网站上提供了一些WebService服务,我们可以对其进行调用

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo中提供了电话归属地查询的使用说明

内容如下:

 

SOAP 1.2

 

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-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 OK
Content-Type: application/soap+xml; charset=utf-8
Content-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

 

以下是 HTTP GET 请求和响应示例。所显示的占位符需替换为实际值。

GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string HTTP/1.1
Host: webservice.webxml.com.cn
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>

HTTP POST

以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length

mobileCode=string&userID=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">string</string>

 

下面为具体实例及代码:

界面显示:

向WebService发送的XML文件: send.xml(放置在SRC路径下)

 

Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <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">  
  3.   <soap12:Body>  
  4.     <getMobileCodeInfo xmlns="http://WebXml.com.cn/">  
  5.       <mobileCode>$number</mobileCode>  
  6.       <userID></userID>  
  7.     </getMobileCodeInfo>  
  8.   </soap12:Body>  
  9. </soap12:Envelope>  

布局文件man.xml

 

 

Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/phoneET"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:inputType="phone" >  
  12.   
  13.         <requestFocus />  
  14.     </EditText>  
  15.   
  16.     <Button  
  17.         android:onClick="onClick"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="归属地查询" />  
  21.   
  22.     <TextView  
  23.         android:id="@+id/locationTV"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:textSize="25sp"  
  27. />  
  28.   
  29. </LinearLayout>  

 

功能清单文件AndroidManifest.xml

 

Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.itheima.webservice"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="10" />  
  8.     <uses-permission android:name="android.permission.INTERNET"/>  
  9.       <instrumentation  
  10.         android:name="android.test.InstrumentationTestRunner"   
  11.         android:targetPackage="com.itheima.webservice"  
  12.         />  
  13.     <application  
  14.         android:icon="@drawable/ic_launcher"  
  15.         android:label="@string/app_name" >  
  16.     <uses-library android:name="android.test.runner" />  
  17.         <activity  
  18.             android:name=".MainActivity"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.   
  23.                 <category android:name="android.intent.category.LAUNCHER" />  
  24.             </intent-filter>  
  25.         </activity>  
  26.     </application>  
  27.   
  28. </manifest>  

MainActivity:

 

 

Java代码 
  1. package com.itheima.webservice;  
  2.   
  3. import com.itheima.webservice.service.NumberService;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     private EditText phoneET;  
  14.     private TextView locationTV;  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         phoneET = (EditText) findViewById(R.id.phoneET);  
  19.        locationTV = (TextView) findViewById(R.id.locationTV);  
  20.           
  21.           
  22.     }  
  23.       
  24.     public void onClick(View view){  
  25.         try {  
  26.             NumberService service = new NumberService();  
  27.             String num = phoneET.getText().toString();  
  28.             String loacation = service.getLocation(num);  
  29.             locationTV.setText(loacation);  
  30.         } catch (Exception e) {  
  31.             e.printStackTrace();  
  32.             Toast.makeText(getApplicationContext(), "查无此号"1).show();  
  33.         }  
  34.     }  
  35.       
  36.       
  37. }  

NumberService
:

 

 

Java代码 
  1. package com.itheima.webservice.service;  
  2.   
  3. import java.io.InputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.URL;  
  6.   
  7. import org.xmlpull.v1.XmlPullParser;  
  8.   
  9. import android.util.Xml;  
  10.   
  11. import com.itheima.webservice.util.StreamUtil;  
  12.   
  13. public class NumberService {  
  14.   
  15.     public String getLocation(String number) throws Exception {  
  16.         // 读取本地准备好的文件, 用输入的号码替换原来的占位符  
  17.                 InputStream in = NumberService.class.getClassLoader().getResourceAsStream("send.xml");  
  18.                 byte[] data = StreamUtil.load(in);  
  19.                 String content = new String(data);  
  20.                 content = content.replace("$number", number);  
  21.                   
  22.                 // 创建连接对象, 设置请求头, 按照Webservice服务端提供的要求来设置  
  23.                 URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");  
  24.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  25.                 conn.setConnectTimeout(5000);  
  26.                 conn.setRequestProperty("Host""webservice.webxml.com.cn");  
  27.                 conn.setRequestProperty("Content-Type""application/soap+xml; charset=utf-8");  
  28.                 conn.setRequestProperty("Content-Length", content.getBytes().length + "");  
  29.                 conn.setRequestMethod("POST");  
  30.                   
  31.                 // 输出数据  
  32.                 conn.setDoOutput(true);  
  33.                 conn.getOutputStream().write(content.getBytes());  
  34.               
  35.                   
  36. //              // 获取服务端传回的数据, 解析XML, 得到结果  
  37.                 XmlPullParser parser = Xml.newPullParser();  
  38.                 parser.setInput(conn.getInputStream(), "UTF-8");  
  39.                   
  40.                 for (int type = parser.getEventType();type!=XmlPullParser.END_DOCUMENT;type=parser.next())   
  41.                   if(type==XmlPullParser.START_TAG&&parser.getName().equals("getMobileCodeInfoResult")){  
  42.                       return parser.nextText();  
  43.                   }  
  44.                 return "没有找到此号码";  
  45.     }  
  46.   
  47. }  

读取输入流工具类StreamUtil:

 

 

Java代码 
  1. package com.itheima.webservice.util;  
  2.   
  3.   
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7.   
  8. public class StreamUtil {  
  9.    public static byte[] load(InputStream in ) throws IOException{  
  10.        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  11.        byte b [] = new byte[1024];  
  12.        int len = -1;  
  13.        while((len=in.read(b))!=-1){  
  14.            baos.write(b,0,len);  
  15.        }  
  16.        baos.close();  
  17.        return baos.toByteArray();  
  18.    }  
  19. }  

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics