手机号码归属地查询
目录结构:
MainActivity
public class MainActivity extends Activity {
private EditText phoneText;
private TextView InfoText;
private AdressService service ;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
service = new AdressService();
phoneText = (EditText) this.findViewById(R.id.phoneNumber);
InfoText = (TextView) this.findViewById(R.id.phone_info);
}
public void query(View v){
Stringphone = phoneText.getText().toString();
try {
Stringinfo = service.getAdress_mobile(phone);
InfoText.setText(info);
}catch (Exception e) {
Toast.makeText(getApplicationContext(),"查询失败", 1).show();
e.printStackTrace();
}
}
}
AdressService
public class AdressService {
/**
* 获取号码信息业务类
* @param mobile 电话号码
* @return 归属地信息
* @throws Exception
*/
public StringgetAdress_mobile(String mobile) throws Exception{
Stringsoap = readsoap(); //获取协议文件
soap= soap.replaceAll("\\$mobile", mobile); //为占位符添加值
Stringpath = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
HttpURLConnectionconn = (HttpURLConnection) new URL(path).openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
conn.setRequestProperty("Content-Length",soap.getBytes().length+"");
conn.getOutputStream().write(soap.getBytes());
if(conn.getResponseCode()== 200){
returnparseSOAP(conn.getInputStream());
}
return null;
}
/**
* pull解析器解析webservice返回的输入流里面所需要的数据
* @param inputStream
* @return 获取的号码信息
* @throws XmlPullParserException
* @throws IOException
*/
private StringparseSOAP(InputStream inputStream) throws XmlPullParserException, IOException {
XmlPullParserparse = Xml.newPullParser();
parse.setInput(inputStream,"UTF-8");
int event =parse.getEventType();
while(event !=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_TAG:
if("getMobileCodeInfoResult".equals(parse.getName())){
return parse.nextText();
}
break;
}
event= parse.next();
}
return null;
}
/**
* 读取内存中SOAP的XML协议文件
* @return SOAP字符串
* @throws Exception
*/
private String readsoap() throws Exception {
InputStreamin = AdressService.class.getClassLoader().getResourceAsStream("soap12.xml");
byte[] data = StreamTool.read(in);
return new String(data,"UTF-8");
}
}
页面布局
</pre><pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号码"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phoneNumber"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="query"
android:text="确定"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/phone_info"
/>
</LinearLayout>
Soap协议详情请看http://www.webxml.com.cn 国内手机号码归属地查询WEB服务里面有API和协议等信息
Soap12.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>
<getMobileCodeInfoxmlns="http://WebXml.com.cn/">
<mobileCode>$mobile</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>