号码归属地查询---------------

发布时间:2016-12-10 14:49:56 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"号码归属地查询---------------",主要涉及到号码归属地查询---------------方面的内容,对于号码归属地查询---------------感兴趣的同学可以参考一下。

1,布局文件main <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="number" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="query" android:text="查询" /> </LinearLayout>2,src/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>$mobile</mobileCode> <userID></userID> </getMobileCodeInfo> </soap12:Body> </soap12:Envelope> 3,mainactivity package com.example.querymobile; import com.itheima.service.QueryService; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_number=(EditText) this.findViewById(R.id.et_number); } public void query(View view){ String number = et_number.getText().toString(); //调用业务方法获取手机号码归属地 try { String address=QueryService.getAddres(number); Toast.makeText(this, "号码归属地为\n"+address, 1).show(); } catch (Exception e) { Toast.makeText(this, "访问失败", 0).show(); e.printStackTrace(); } } } 3,queryservice package com.itheima.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import com.itheima.util.NumberParser; import android.view.View; import android.widget.EditText; public class QueryService { /** * 根据号码调用webservice获取手机号码归属地 * @param number 电话号码 * @return * @throws Exception */ public static String getAddres(String number) throws Exception{ InputStream inputStream = QueryService.class.getClassLoader().getResourceAsStream("post.xml"); byte[] b=StreamTool.getbyte(inputStream); String xml=new String(b); String postxml = xml.replace("$mobile", number); System.out.println(postxml); String address = sendPostManage(postxml); return address; } public static String sendPostManage(String postxml) throws Exception{ String path="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; URL url=new URL(path); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); String content=postxml; byte[] bytes = content.getBytes(); conn.setRequestProperty("Content-Lengt", bytes.length+""); //允许http协议对外输出信息 conn.setDoOutput(true); //把数据写给服务器 conn.getOutputStream().write(bytes); if(conn.getResponseCode()==200){ InputStream in = conn.getInputStream(); /*byte[] getbyte = StreamTool.getbyte(in); System.out.println(new String(getbyte)); //这里打印的是返回的xml */ return NumberParser.getXmlInfo(in); } return "访问网络错误出错"; } } 4,streamtool工具 package com.itheima.service; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import android.graphics.Bitmap; public class StreamTool { public static byte[] getbyte(InputStream in) throws Exception{ ByteArrayOutputStream out=new ByteArrayOutputStream(); byte [] b=new byte[1024]; int i=0; while((i=in.read(b))!=-1){ out.write(b, 0, i); } in.close(); out.flush(); return out.toByteArray(); } } 5,numberparser package com.itheima.util; import java.io.InputStream; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.R.xml; import android.util.Xml; /*<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"> <getMobileCodeInfoResult>13269012448:北京 北京 北京联通GSM卡</getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap:Body> </soap:Envelope>*/ public class NumberParser { //获取xml手机的归属地 public static String getXmlInfo(InputStream is) throws Exception{ XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, "utf-8"); int type=parser.getEventType(); while(type!=XmlPullParser.END_DOCUMENT){ switch (type) { case XmlPullParser.START_TAG: if("getMobileCodeInfoResult".equals(parser.getName())){ return parser.nextText(); } break; } type=parser.next(); } return "查无此号"; } } 6,Test package com.itheima.test; import com.itheima.service.QueryService; import android.test.AndroidTestCase; public class TestWebService extends AndroidTestCase { public void TestGetAddress() throws Exception{ QueryService.getAddres("13269012448"); } }

上一篇:linux下的C语言开发(多线程编程)
下一篇:自醒-【德鲁克的经典五问】

相关文章

相关评论