public class MobileService {
/**
* 获取手机号归属地
* @param mobile
* @return
* @throws Exception
*/
public static String getAddress(String mobile) throws Exception{
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
InputStream insInputStream = MobileService.class.getClassLoader().getResourceAsStream("soap.xml");
String soap = readFile(insInputStream);
soap = soap.replaceAll("\\$mobile", mobile);
byte[]data = soap.getBytes();
HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
connection.setRequestProperty("Content-Length", String.valueOf(data.length));
connection.getOutputStream().write(data);
if(connection.getResponseCode()==200){
return parseXML(connection.getInputStream());
}
return null;
}
/**
* 解析返回的SOAP协议
* @param inputStream
* @return
*/
private static String parseXML(InputStream inputStream)throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream,"UTF-8");
int event = parser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_TAG:
if("getMobileCodeInfoResult".equals(parser.getName())){
return parser.nextText();
}
break;
}
event = parser.next();
}
return null;
}
private static String readFile(InputStream insInputStream) throws Exception {
byte[] data = StreamTool.read(insInputStream);
return new String(data);
}
}
public class StreamTool {
/**
* 读取输入流数据
* @param inStream
* @return
*/
public static byte[] read(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/inputnum"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/number"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/find"
android:id="@+id/button"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/address"
/>
</LinearLayout>
public class MainActivity extends Activity {
private EditText mobileText;
private TextView addressView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mobileText = (EditText) this.findViewById(R.id.number);
addressView = (TextView) this.findViewById(R.id.address);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener());
}
private final class ButtonClickListener implements View.OnClickListener{
public void onClick(View v) {
String mobile = mobileText.getText().toString();
try {
String address = MobileService.getAddress(mobile);
addressView.setText(address);
Toast.makeText(getApplicationContext(), R.string.success, 1).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.failed, 1).show();
}
}
}
}