这个小程序的主要原理是通过HTTP协议发送XML数据并调用webservice,分析返回的数据来进行Android
查询QQ
是否在线
。
StreamTool.java:
Web Service(WEB服务)能够快捷和方便地综合并结合各种系统、商务和任何应用平台。新出现的 Web Services 标准: SOAP、WSDL 和 UDDI 能够使任何系统和系统之间的应用变为更加方便和廉价。
Web服务(Web Services)和Service—Oriented Architecture作为实现分布式系统和履行公司内部、公司之间的应用整合的技术和架构出现。SOA和Web服务的体系结构是两个不同层面的问题,前者是概念模式,面向商业应用;后者则是实现模式,面向技术框架。
面向服务的体系结构所表示的是一个概念上的模型,在这个模型中,松散耦合的应用在网络上被描述、发布和调用。而Web服务则是一组由协议构成的协议栈所定义的框架结构,它定义了在不同的系统之间通信松散耦合的编程框架。也可以认为,Web服务体系结构实际上是面向服务的体系结构的一个特定实现;面向服务的体系结构作为一个概念上的模型,将网络、传输协议以及安全等具体的细节都遗留给特定的实现。Web服务中的SOAP,WSDL等都是具体实现细节的标准。
第一步:新建一个Android工程命名为QQStatus目录结构如下图:
首先写出布局
1
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
2 xmlns:tools ="http://schemas.android.com/tools"
3 android:layout_width ="fill_parent"
4 android:layout_height ="fill_parent"
5 android:orientation ="vertical"
6 tools:context =".MainActivity" >
7
8 < TextView
9 android:layout_width ="fill_parent"
10 android:layout_height ="wrap_content"
11 android:layout_margin ="5dp"
12 android:text ="@string/qq"
13 android:textSize ="15sp" />
14
15 < EditText
16 android:id ="@+id/et_qq_code"
17 android:layout_width ="fill_parent"
18 android:layout_height ="wrap_content"
19 android:layout_margin ="5dp"
20 android:inputType ="number"
21 android:singleLine ="true"
22 android:text ="@string/code" />
23
24 < Button
25 android:id ="@+id/btn_check_online"
26 android:layout_width ="wrap_content"
27 android:layout_height ="wrap_content"
28 android:layout_margin ="5dp"
29 android:text ="@string/check_online" />
30
31 < TextView
32 android:id ="@+id/tv_result"
33 android:layout_width ="fill_parent"
34 android:layout_height ="wrap_content"
35 android:layout_margin ="5dp"
36 android:textSize ="15sp" />
37
38 </ LinearLayout >
2 xmlns:tools ="http://schemas.android.com/tools"
3 android:layout_width ="fill_parent"
4 android:layout_height ="fill_parent"
5 android:orientation ="vertical"
6 tools:context =".MainActivity" >
7
8 < TextView
9 android:layout_width ="fill_parent"
10 android:layout_height ="wrap_content"
11 android:layout_margin ="5dp"
12 android:text ="@string/qq"
13 android:textSize ="15sp" />
14
15 < EditText
16 android:id ="@+id/et_qq_code"
17 android:layout_width ="fill_parent"
18 android:layout_height ="wrap_content"
19 android:layout_margin ="5dp"
20 android:inputType ="number"
21 android:singleLine ="true"
22 android:text ="@string/code" />
23
24 < Button
25 android:id ="@+id/btn_check_online"
26 android:layout_width ="wrap_content"
27 android:layout_height ="wrap_content"
28 android:layout_margin ="5dp"
29 android:text ="@string/check_online" />
30
31 < TextView
32 android:id ="@+id/tv_result"
33 android:layout_width ="fill_parent"
34 android:layout_height ="wrap_content"
35 android:layout_margin ="5dp"
36 android:textSize ="15sp" />
37
38 </ LinearLayout >
然后建立web服务的xml,qqstatus.xml
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 < qqCheckOnline xmlns ="http://WebXml.com.cn/" >
5 < qqCode > $qq </ qqCode >
6 </ qqCheckOnline >
7 </ soap12:Body >
8 </ soap12:Envelope >
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 < qqCheckOnline xmlns ="http://WebXml.com.cn/" >
5 < qqCode > $qq </ qqCode >
6 </ qqCheckOnline >
7 </ soap12:Body >
8 </ soap12:Envelope >
Service方法:
1
package
com.gaolei.qqstatus.service;
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5 import java.net.HttpURLConnection;
6 import java.net.URL;
7
8 import org.xmlpull.v1.XmlPullParser;
9
10 import android.util.Xml;
11
12 import com.gaolei.qqstatus.util.StreamTool;
13
14 public class QQService {
15
16 /**
17 * 获取QQ状态
18 * @param qq QQ号码 Y = 在线;N = 离线;E = QQ号码错误;A = 商业用户验证失败;V = 免费用户超过数量
19 * @return
20 * @throws Exception
21 */
22 public static String getQQStatus(String qq) throws Exception {
23 InputStream inputStream = QQService. class .getClassLoader()
24 .getResourceAsStream( " qqstatus.xml " );
25 byte [] data = StreamTool.readInputStream(inputStream);
26 String xml = new String(data, " UTF-8 " );
27 String soapEntity = xml.replaceAll( " \\$qq " , qq);
28 data = soapEntity.getBytes();
29 String path = " http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx " ;
30 URL url = new URL(path);
31 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
32 conn.setDoOutput( true );
33 conn.setConnectTimeout( 5000 );
34 conn.setRequestMethod( " POST " );
35 conn.setRequestProperty( " Content-Type " ,
36 " application/soap+xml;charset=utf-8 " );
37 conn.setRequestProperty( " Content-Length " , String.valueOf(data.length));
38 OutputStream outputStream = conn.getOutputStream();
39 outputStream.write(data);
40 outputStream.flush();
41 outputStream.close();
42 if (conn.getResponseCode() == 200 ) {
43 InputStream responseStream = conn.getInputStream();
44 return parseXml(responseStream);
45 }
46 return null ;
47 }
48
49 /**
50 * 解析XML
51 * @param responseStream 输入流
52 * @return
53 * @throws Exception
54 */
55 private static String parseXml(InputStream responseStream) throws Exception {
56 XmlPullParser parser = Xml.newPullParser();
57 parser.setInput(responseStream, " UTF-8 " );
58 int event = parser.getEventType();
59 while (event != XmlPullParser.END_DOCUMENT) {
60 switch (event) {
61 case XmlPullParser.START_TAG:
62 if ( " qqCheckOnlineResult " .equals(parser.getName())) {
63 return parser.nextText();
64 }
65 break ;
66 }
67 event = parser.next();
68 }
69 return null ;
70 }
71 }
72
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5 import java.net.HttpURLConnection;
6 import java.net.URL;
7
8 import org.xmlpull.v1.XmlPullParser;
9
10 import android.util.Xml;
11
12 import com.gaolei.qqstatus.util.StreamTool;
13
14 public class QQService {
15
16 /**
17 * 获取QQ状态
18 * @param qq QQ号码 Y = 在线;N = 离线;E = QQ号码错误;A = 商业用户验证失败;V = 免费用户超过数量
19 * @return
20 * @throws Exception
21 */
22 public static String getQQStatus(String qq) throws Exception {
23 InputStream inputStream = QQService. class .getClassLoader()
24 .getResourceAsStream( " qqstatus.xml " );
25 byte [] data = StreamTool.readInputStream(inputStream);
26 String xml = new String(data, " UTF-8 " );
27 String soapEntity = xml.replaceAll( " \\$qq " , qq);
28 data = soapEntity.getBytes();
29 String path = " http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx " ;
30 URL url = new URL(path);
31 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
32 conn.setDoOutput( true );
33 conn.setConnectTimeout( 5000 );
34 conn.setRequestMethod( " POST " );
35 conn.setRequestProperty( " Content-Type " ,
36 " application/soap+xml;charset=utf-8 " );
37 conn.setRequestProperty( " Content-Length " , String.valueOf(data.length));
38 OutputStream outputStream = conn.getOutputStream();
39 outputStream.write(data);
40 outputStream.flush();
41 outputStream.close();
42 if (conn.getResponseCode() == 200 ) {
43 InputStream responseStream = conn.getInputStream();
44 return parseXml(responseStream);
45 }
46 return null ;
47 }
48
49 /**
50 * 解析XML
51 * @param responseStream 输入流
52 * @return
53 * @throws Exception
54 */
55 private static String parseXml(InputStream responseStream) throws Exception {
56 XmlPullParser parser = Xml.newPullParser();
57 parser.setInput(responseStream, " UTF-8 " );
58 int event = parser.getEventType();
59 while (event != XmlPullParser.END_DOCUMENT) {
60 switch (event) {
61 case XmlPullParser.START_TAG:
62 if ( " qqCheckOnlineResult " .equals(parser.getName())) {
63 return parser.nextText();
64 }
65 break ;
66 }
67 event = parser.next();
68 }
69 return null ;
70 }
71 }
72
StreamTool.java:
1
package
com.gaolei.qqstatus.util;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.InputStream;
5
6 public class StreamTool {
7
8 /**
9 * 读取输入流数据
10 * @param inputStream
11 * @return
12 * @throws Exception
13 */
14 public static byte [] readInputStream(InputStream inputStream)
15 throws Exception {
16 ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
17 byte [] buffer = new byte [ 1024 ];
18 int len = - 1 ;
19 while ((len = inputStream.read(buffer)) != - 1 ) {
20 outSteam.write(buffer, 0 , len);
21 }
22 outSteam.close();
23 inputStream.close();
24 return outSteam.toByteArray();
25 }
26
27 }
28
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.InputStream;
5
6 public class StreamTool {
7
8 /**
9 * 读取输入流数据
10 * @param inputStream
11 * @return
12 * @throws Exception
13 */
14 public static byte [] readInputStream(InputStream inputStream)
15 throws Exception {
16 ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
17 byte [] buffer = new byte [ 1024 ];
18 int len = - 1 ;
19 while ((len = inputStream.read(buffer)) != - 1 ) {
20 outSteam.write(buffer, 0 , len);
21 }
22 outSteam.close();
23 inputStream.close();
24 return outSteam.toByteArray();
25 }
26
27 }
28
Activity:
1
package
com.gaolei.qqstatus.activity;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.EditText;
10 import android.widget.TextView;
11 import android.widget.Toast;
12
13 import com.gaolei.qqstatus.R;
14 import com.gaolei.qqstatus.service.QQService;
15
16 public class MainActivity extends Activity implements OnClickListener {
17 private static final String TAG = " MainActivity " ;
18
19 private Button mCheckOnlineButton;
20 private EditText mQQCodeEditText;
21 private TextView mResultTextView;
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super .onCreate(savedInstanceState);
26 setContentView(R.layout.activity_main);
27 findViews();
28 bindViews();
29 }
30
31 private void findViews() {
32 mCheckOnlineButton = (Button) findViewById(R.id.btn_check_online);
33 mQQCodeEditText = (EditText) findViewById(R.id.et_qq_code);
34 mResultTextView = (TextView) findViewById(R.id.tv_result);
35 }
36
37 private void bindViews() {
38 mCheckOnlineButton.setOnClickListener( this );
39 }
40
41 @Override
42 public void onClick(View v) {
43 String qqCode = mQQCodeEditText.getText().toString().trim();
44 if ( ! "" .equals(qqCode)) {
45 try {
46 String result = QQService.getQQStatus(qqCode);
47 Log.i(TAG, result);
48 if (result.equals( " Y " )) {
49 mResultTextView.setText( " 在线 " );
50 } else if (result.equals( " N " )) {
51 mResultTextView.setText( " 不在线 " );
52 }
53 } catch (Exception e) {
54 Log.e(TAG, e.toString());
55 Toast.makeText(MainActivity. this , R.string.error,
56 Toast.LENGTH_LONG).show();
57 }
58 }
59 }
60
61 }
62
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.EditText;
10 import android.widget.TextView;
11 import android.widget.Toast;
12
13 import com.gaolei.qqstatus.R;
14 import com.gaolei.qqstatus.service.QQService;
15
16 public class MainActivity extends Activity implements OnClickListener {
17 private static final String TAG = " MainActivity " ;
18
19 private Button mCheckOnlineButton;
20 private EditText mQQCodeEditText;
21 private TextView mResultTextView;
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super .onCreate(savedInstanceState);
26 setContentView(R.layout.activity_main);
27 findViews();
28 bindViews();
29 }
30
31 private void findViews() {
32 mCheckOnlineButton = (Button) findViewById(R.id.btn_check_online);
33 mQQCodeEditText = (EditText) findViewById(R.id.et_qq_code);
34 mResultTextView = (TextView) findViewById(R.id.tv_result);
35 }
36
37 private void bindViews() {
38 mCheckOnlineButton.setOnClickListener( this );
39 }
40
41 @Override
42 public void onClick(View v) {
43 String qqCode = mQQCodeEditText.getText().toString().trim();
44 if ( ! "" .equals(qqCode)) {
45 try {
46 String result = QQService.getQQStatus(qqCode);
47 Log.i(TAG, result);
48 if (result.equals( " Y " )) {
49 mResultTextView.setText( " 在线 " );
50 } else if (result.equals( " N " )) {
51 mResultTextView.setText( " 不在线 " );
52 }
53 } catch (Exception e) {
54 Log.e(TAG, e.toString());
55 Toast.makeText(MainActivity. this , R.string.error,
56 Toast.LENGTH_LONG).show();
57 }
58 }
59 }
60
61 }
62
修改AndroidManifest.xml文件,添加访问网络权限:
strings.xml:
1
<
uses-permission
android:name
="android.permission.INTERNET"
/>
strings.xml:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < resources >
3
4 < string name ="app_name" > QQ在线查询器 </ string >
5 < string name ="qq" > QQ号码 </ string >
6 < string name ="code" > 423536390 </ string >
7 < string name ="check_online" > 查询状态 </ string >
8 < string name ="error" > 获取状态失败! </ string >
9
10 </ resources >
2 < resources >
3
4 < string name ="app_name" > QQ在线查询器 </ string >
5 < string name ="qq" > QQ号码 </ string >
6 < string name ="code" > 423536390 </ string >
7 < string name ="check_online" > 查询状态 </ string >
8 < string name ="error" > 获取状态失败! </ string >
9
10 </ resources >