鱼C论坛

 找回密码
 立即注册
查看: 2381|回复: 1

android学习之webServiceXml--实现电话号码的归属地查询

[复制链接]
发表于 2014-11-25 15:47:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本案例实现的是通过互联网实现对号码的归属地的查询,包括省份,城市,电话卡的类型!注意:在查询的时候,我用的是异步任务,这样的话主线程就不容易发生阻塞了;

1.所需的权限:
  1. <uses-permission android:name="android.permission.INTERNET"/>
复制代码

2.布局文件:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     tools:context=".MainActivity" >

  7.     <EditText
  8.         
  9.         android:id="@+id/mobilePhone"
  10.         android:layout_width="fill_parent"
  11.         android:layout_height="wrap_content"
  12.           android:hint="请输入电话号码"
  13.         
  14.         />
  15.     <Button
  16.         android:id="@+id/findPhone"
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content"
  19.         android:text="查询"
  20.         android:onClick="findPhone"
  21.         />
  22.     <TextView
  23.         android:id="@+id/resultInfo"
  24.         android:layout_width="wrap_content"
  25.         android:layout_height="wrap_content"
  26.         android:text="归属地查询" />

  27. </LinearLayout>
复制代码

2.要发送出去的xml文件,这里要说明的是,要想查询到数据的话,得向服务端发一个xml文件,然后服务端才会返回一个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.     <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
  5.       <mobileCode>phone</mobileCode>
  6.       <userID></userID>
  7.     </getMobileCodeInfo>
  8.   </soap12:Body>
  9. </soap12:Envelope>
复制代码
3.查询所需要的操作的类:
  1. package com.example.webservice_test.utils;

  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;

  7. import org.xmlpull.v1.XmlPullParser;
  8. import org.xmlpull.v1.XmlPullParserException;

  9. import android.util.Xml;

  10. import com.example.webservice_test.MainActivity;

  11. public class WebServiceUtils {

  12.         public static String readXml() throws Exception
  13.         {
  14.                 ByteArrayOutputStream bao = new ByteArrayOutputStream();
  15.                 //获得类的加载器,得到xml文件的输入流
  16.                 InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("phonexml.xml");
  17.                 byte[] buffer = new byte[1024];   //输入流的缓冲区
  18.                 int len = -1;  //标志
  19.                
  20.                 while((len = is.read(buffer)) != -1)  //只要不等于-1的话就循环字节数组输出流中,其实也是在内存中
  21.                 {
  22.                         bao.write(buffer, 0, len);
  23.                 }
  24.                 is.close();
  25.                 bao.close();
  26.                 return new String(bao.toByteArray());
  27.         }
  28.        
  29.         public static String getPhoneNumber(InputStream is) throws Exception
  30.         {
  31.                 XmlPullParser parser = Xml.newPullParser();  //实例化XnlPullParser解析器
  32.                 parser.setInput(is, "utf-8");
  33.                
  34.                 //循环如果开始标签等于getMobileCodeInfoResult的话就返回里面的内容
  35.                 for(int event=parser.getEventType(); event!=XmlPullParser.END_DOCUMENT; event=parser.next())
  36.                 {
  37.                         if(event== XmlPullParser.START_TAG && parser.getName().equals("getMobileCodeInfoResult"))
  38.                         {
  39.                                 return parser.nextText();
  40.                         }
  41.                 }
  42.                 return null;
  43.                
  44.         }
  45.        
  46.         public static String QueryPhoneArea(String path, String mobileNumber) throws Exception
  47.         {
  48.                
  49.                 String data = readXml().replace("phone", mobileNumber);
  50.                 URL url = new URL(path);  //链接地址
  51.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();  //打开链接
  52.                 conn.setRequestMethod("POST");  //请求方法
  53.                 conn.setConnectTimeout(5000);   //设置超时的时间
  54.                 conn.setRequestProperty("Host", "webservice.webxml.com.cn");
  55.                 conn.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
  56.                 conn.setRequestProperty("Content-Length", data.length()+"");
  57.                 conn.getOutputStream().write(data.getBytes()); //将电话号码发出去
  58.                 int code = conn.getResponseCode();
  59.                
  60.                 if(code == 200)
  61.                 {
  62.                         InputStream is = conn.getInputStream();   //得到发回来的输入流
  63.                         return getPhoneNumber(is);   //获得信息
  64.                 }
  65.                 return null;
  66.                
  67.         }
  68. }
复制代码

4.主程序的实现:
  1. package com.example.webservice_test;

  2. import com.example.webservice_test.utils.WebServiceUtils;

  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;

  12. public class MainActivity extends Activity {

  13.         private EditText editText;
  14.         private TextView textView;
  15.         private String path;
  16.         private String mobileNumber;
  17.         @Override
  18.         protected void onCreate(Bundle savedInstanceState) {
  19.                 super.onCreate(savedInstanceState);
  20.                 setContentView(R.layout.activity_main);
  21.                
  22.                 this.editText = (EditText) this.findViewById(R.id.mobilePhone);
  23.                 this. textView= (TextView) this.findViewById(R.id.resultInfo);
  24.         }

  25.         public void findPhone(View v)
  26.         {
  27.                 this.mobileNumber = this.editText.getText().toString();
  28.                 Log.i("MainActivity", this.mobileNumber);
  29.                 //所要访问的地址
  30.                 this.path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
  31.                
  32.                 if(mobileNumber != null && !"".equals(mobileNumber))
  33.                 {
  34.                         findPhoneNumber();
  35.                 }else
  36.                 {
  37.                         Toast.makeText(this,"请输入电话号码" +  this.mobileNumber, Toast.LENGTH_SHORT).show();
  38.                 }
  39.                
  40.         }
  41.        
  42.         public void findPhoneNumber()
  43.         {
  44.                 new AsyncTask<Void, Void, String>(){   //异步任务

  45.                         @Override
  46.                         protected void onPreExecute() {
  47.                                 // TODO Auto-generated method stub
  48.                                 super.onPreExecute();
  49.                                 textView.setText("正在查询中...");
  50.                         }
  51.                        
  52.                         @Override
  53.                         protected String doInBackground(Void... arg0) {
  54.                                 // TODO Auto-generated method stub
  55.                                 try
  56.                                 {
  57.                                         return WebServiceUtils.QueryPhoneArea(path, mobileNumber);
  58.                                 }catch(Exception e)
  59.                                 {
  60.                                         e.printStackTrace();
  61.                                         return null;
  62.                                 }
  63.                                
  64.                         }

  65.                         @Override
  66.                         protected void onPostExecute(String result) {
  67.                                 // TODO Auto-generated method stub
  68.                                 textView.setText(result);
  69.                                 super.onPostExecute(result);
  70.                         }       
  71.                        
  72.                 }.execute();
  73.         }
  74. }
复制代码

QQ截图20141125154736.png


评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
拈花小仙 + 5 + 5 + 3 感谢楼主无私奉献!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-11-25 22:34:34 | 显示全部楼层
你这是安卓的app ,发错地方了吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 03:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表