|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本案例实现的是通过互联网实现对号码的归属地的查询,包括省份,城市,电话卡的类型!注意:在查询的时候,我用的是异步任务,这样的话主线程就不容易发生阻塞了;
1.所需的权限:
- <uses-permission android:name="android.permission.INTERNET"/>
复制代码
2.布局文件:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <EditText
-
- android:id="@+id/mobilePhone"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="请输入电话号码"
-
- />
- <Button
- android:id="@+id/findPhone"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="查询"
- android:onClick="findPhone"
- />
- <TextView
- android:id="@+id/resultInfo"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="归属地查询" />
- </LinearLayout>
复制代码
2.要发送出去的xml文件,这里要说明的是,要想查询到数据的话,得向服务端发一个xml文件,然后服务端才会返回一个xml的结果;
- <?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>phone</mobileCode>
- <userID></userID>
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
复制代码 3.查询所需要的操作的类:
- package com.example.webservice_test.utils;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import org.xmlpull.v1.XmlPullParser;
- import org.xmlpull.v1.XmlPullParserException;
- import android.util.Xml;
- import com.example.webservice_test.MainActivity;
- public class WebServiceUtils {
- public static String readXml() throws Exception
- {
- ByteArrayOutputStream bao = new ByteArrayOutputStream();
- //获得类的加载器,得到xml文件的输入流
- InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("phonexml.xml");
- byte[] buffer = new byte[1024]; //输入流的缓冲区
- int len = -1; //标志
-
- while((len = is.read(buffer)) != -1) //只要不等于-1的话就循环字节数组输出流中,其实也是在内存中
- {
- bao.write(buffer, 0, len);
- }
- is.close();
- bao.close();
- return new String(bao.toByteArray());
- }
-
- public static String getPhoneNumber(InputStream is) throws Exception
- {
- XmlPullParser parser = Xml.newPullParser(); //实例化XnlPullParser解析器
- parser.setInput(is, "utf-8");
-
- //循环如果开始标签等于getMobileCodeInfoResult的话就返回里面的内容
- for(int event=parser.getEventType(); event!=XmlPullParser.END_DOCUMENT; event=parser.next())
- {
- if(event== XmlPullParser.START_TAG && parser.getName().equals("getMobileCodeInfoResult"))
- {
- return parser.nextText();
- }
- }
- return null;
-
- }
-
- public static String QueryPhoneArea(String path, String mobileNumber) throws Exception
- {
-
- String data = readXml().replace("phone", mobileNumber);
- URL url = new URL(path); //链接地址
- HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //打开链接
- conn.setRequestMethod("POST"); //请求方法
- conn.setConnectTimeout(5000); //设置超时的时间
- conn.setRequestProperty("Host", "webservice.webxml.com.cn");
- conn.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
- conn.setRequestProperty("Content-Length", data.length()+"");
- conn.getOutputStream().write(data.getBytes()); //将电话号码发出去
- int code = conn.getResponseCode();
-
- if(code == 200)
- {
- InputStream is = conn.getInputStream(); //得到发回来的输入流
- return getPhoneNumber(is); //获得信息
- }
- return null;
-
- }
- }
复制代码
4.主程序的实现:
- package com.example.webservice_test;
- import com.example.webservice_test.utils.WebServiceUtils;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.app.Activity;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private EditText editText;
- private TextView textView;
- private String path;
- private String mobileNumber;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- this.editText = (EditText) this.findViewById(R.id.mobilePhone);
- this. textView= (TextView) this.findViewById(R.id.resultInfo);
- }
- public void findPhone(View v)
- {
- this.mobileNumber = this.editText.getText().toString();
- Log.i("MainActivity", this.mobileNumber);
- //所要访问的地址
- this.path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
-
- if(mobileNumber != null && !"".equals(mobileNumber))
- {
- findPhoneNumber();
- }else
- {
- Toast.makeText(this,"请输入电话号码" + this.mobileNumber, Toast.LENGTH_SHORT).show();
- }
-
- }
-
- public void findPhoneNumber()
- {
- new AsyncTask<Void, Void, String>(){ //异步任务
- @Override
- protected void onPreExecute() {
- // TODO Auto-generated method stub
- super.onPreExecute();
- textView.setText("正在查询中...");
- }
-
- @Override
- protected String doInBackground(Void... arg0) {
- // TODO Auto-generated method stub
- try
- {
- return WebServiceUtils.QueryPhoneArea(path, mobileNumber);
- }catch(Exception e)
- {
- e.printStackTrace();
- return null;
- }
-
- }
- @Override
- protected void onPostExecute(String result) {
- // TODO Auto-generated method stub
- textView.setText(result);
- super.onPostExecute(result);
- }
-
- }.execute();
- }
- }
复制代码
|
评分
-
查看全部评分
|