data.xml文件
$mobile
布局文件:searchaddress.xml
核心代码:SearchAddressActivity.java
public class SearchAddressActivity extends Activity {
private EditText et_number;
private Button bt_search;
private TextView tv_number;
private TextView tv_belong_to;
private TextView tv_city;
private ProgressDialog pd;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
pd.dismiss();
//13912345678:江苏 常州 江苏移动全球通卡(注意冒号是中文的冒号)
String result=(String)msg.obj;
if(result.length()<15){
Toast.makeText(getApplicationContext(), "您输入的手机号码有误", 1).show();
return;
}
et_number.setText("");
String number=result.substring(0, result.indexOf(":"));
tv_number.setText(number);
String belong_to=result.substring(result.lastIndexOf(" ")+1);
tv_belong_to.setText(belong_to);
String city=result.substring(result.indexOf(":")+1, result.lastIndexOf(" "));
tv_city.setText(city);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchaddress);
et_number = (EditText) this.findViewById(R.id.et_number);
bt_search = (Button) this.findViewById(R.id.bt_search);
tv_number = (TextView) this.findViewById(R.id.tv_number);
tv_belong_to = (TextView) this.findViewById(R.id.tv_belong_to);
tv_city = (TextView) this.findViewById(R.id.tv_city);
pd=new ProgressDialog(this);
}
public void click(View v) {
// 查询归属地的操作是耗时操作,要使用多线程。。。
String number = et_number.getText().toString().trim();
if (TextUtils.isEmpty(number)) {
Toast.makeText(getApplicationContext(), "号码不能为空", 1).show();
return;
}
if(number.length()!=11){
Toast.makeText(getApplicationContext(), "手机号码应为11位数字", 1).show();
return;
}
new Thread(new MyRunnable()).start();
pd.setTitle("查询中....");
pd.show();
}
class MyRunnable implements Runnable {
public void run() {
try {
URL url = new URL(
"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
InputStream in = getClass().getClassLoader()
.getResourceAsStream("data.xml");
byte[] data = StreamTool.getBytes(in);
String str = new String(data);
String content = str.replace("$mobile", et_number.getText()
.toString().trim());
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
"application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", content.length() + "");
conn.getOutputStream().write(content.getBytes());
InputStream response=conn.getInputStream();
String result=parseXml(response);
Message msg=new Message();
msg.obj=result;
handler.sendMessage(msg);
} catch (Exception e) {
pd.dismiss();
//在子线程中使用Toast
Looper.prepare();
Toast.makeText(getApplicationContext(), "网络异常!请检查您的网络设置!", 1).show();
Looper.loop();
e.printStackTrace();
}
}
}
private String parseXml(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 "查无此号";
}
}