android手机号归属地查下案例源码

先提供源码,太晚没空添加备注

完整源码:

 1 package caicai.cn.mobile;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.InputStream;
 5 import java.net.HttpURLConnection;
 6 import java.net.URL;
 7 import org.xmlpull.v1.XmlPullParser;
 8 import android.util.Xml;
 9 
10 public class address_service {
11     public static String getaddress(String moblie) throws Exception {
12         String soap = readsoap();
13         soap = soap.replaceAll("\\$mobile", moblie).trim();
14         byte[] entity = soap.getBytes();
15         String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
16         HttpURLConnection conn = (HttpURLConnection) new URL(path)
17                 .openConnection();
18         conn.setReadTimeout(5000);
19         conn.setRequestMethod("POST");
20         conn.setDoOutput(true);
21         conn.setRequestProperty("Content-Type",
22                 "application/soap+xml; charset=utf-8");
23         conn.setRequestProperty("Content-Lenght", String.valueOf(entity.length));
24         conn.getOutputStream().write(entity);
25         
26         if (conn.getResponseCode() == 200) {
27             return parsesoap(conn.getInputStream());
28         }
29         return null;
30     }
31 
32     private static String parsesoap(InputStream xml) throws Exception {
33         XmlPullParser pullparser = Xml.newPullParser();
34         pullparser.setInput(xml, "UTF-8");
35         int event = pullparser.getEventType();
36         while (event != XmlPullParser.END_DOCUMENT) {
37             switch (event) {
38             case XmlPullParser.START_TAG:
39                 if ("getMobileCodeInfoResult".equals(pullparser.getName())) {
40                     
41                     return pullparser.nextText();
42                 }
43                 break;
44             default:
45                 break;
46             }
47             event = pullparser.next();
48         }
49         return null;
50     }
51 
52     private static String readsoap() throws Exception {
53         InputStream instream = address_service.class.getClassLoader()
54                 .getResourceAsStream("soap12.xml");
55         ByteArrayOutputStream outstream = new ByteArrayOutputStream();
56         byte[] buffer = new byte[1024];
57         while (instream.read(buffer) != -1) {
58             outstream.write(buffer);
59         }
60         instream.close();
61         byte[] data = outstream.toByteArray();
62         return new String(data);
63     }
64 }
address_service。java
 1 package caicai.cn.mobile;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.EditText;
 7 import android.widget.TextView;
 8 import android.widget.Toast;
 9 
10 public class Cai_mobile_addressActivity extends Activity {
11     EditText mobiletext;
12     TextView textview;
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.main);
16         mobiletext=(EditText) findViewById(R.id.mobile);
17         textview=  (TextView) findViewById(R.id.textview);
18     }   
19     public void search(View v){
20     String mobile=    mobiletext.getText().toString();
21         try {
22         String address=    address_service.getaddress(mobile);
23             textview.setText(address);        
24         } catch (Exception e) {
25             Toast.makeText(getApplicationContext(), "出错了", 1).show();
26             e.printStackTrace();
27         }
28     }
29 }
Cai_mobile_addressActivity()
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>$mobile</mobileCode>
6       <userID></userID>
7     </getMobileCodeInfo>
8   </soap12:Body>
9 </soap12:Envelope>
soap12.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="caicai.cn.mobile"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk android:minSdkVersion="8" />
 8     <uses-permission android:name="android.permission.INTERNET"/>
 9 
10     <application
11         android:icon="@drawable/ic_launcher"
12         android:label="@string/app_name" >
13         <activity
14             android:label="@string/app_name"
15             android:name=".Cai_mobile_addressActivity" >
16             <intent-filter >
17                 <action android:name="android.intent.action.MAIN" />
18 
19                 <category android:name="android.intent.category.LAUNCHER" />
20             </intent-filter>
21         </activity>
22     </application>
23 
24 </manifest>
AndroidManifest.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="手机号:" />
11 
12     <EditText
13         android:id="@+id/mobile"
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content" />
16 
17     <Button
18         android:id="@+id/button1"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:text="查询" 
22         android:onClick="search"/>
23     
24     <TextView
25         android:id="@+id/textview"
26         android:layout_width="fill_parent"
27         android:layout_height="wrap_content"
28        />
29 </LinearLayout>
main.xml

 

posted on 2013-06-23 23:19  clarenceV1  阅读(390)  评论(0编辑  收藏  举报

导航