public class SearchLocationTask extends AsyncTask{
private String methodName;
private String soapAction;
private String phoneNum;
private String getPropertyName;
public SearchLocationTask(String phoneNum){
this.methodName = Constant.GET_LOCATION_METHOD_NAME;
this.soapAction = Constant.GET_LOCATION_SOAP_ACTION;
this.phoneNum= phoneNum;
this.getPropertyName = Constant.GET_LOCATION_RESULT_NAME;
}
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
SoapObject detail = null;
Object result = null;
try{
SoapObject rpc = new SoapObject(Constant.NAMESPACE, methodName);
rpc.addProperty("mobileCode", phoneNum);
Looper.prepare();
HttpTransportSE ht = new HttpTransportSE(Constant.URL);
ht.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
ht.call(soapAction, envelope);
detail = (SoapObject) envelope.bodyIn;
result = detail.getProperty(getPropertyName);
}catch(Exception e){
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
/**
* 解析数据
*/
String resultStr = result.toString();
String[] resultList = resultStr.split(" ");
if(resultList.length == 1){
//获取字符串汇中的中文列表
Util.showMessageByToast(currentContext, resultList[0]);
}else{
//获取省份名
String province = Constant.GetChineseWord(resultList[0]).get(0);
//获取城市名
String city = resultList[1];
int length = province.length()+2;
//获取运营商
String service = resultList[2].substring(0,length);
//获取卡类型
String cardType = resultList[2].substring(length);
//展示
//......
}
}
}
public class Constant {
/**
* 命名空间
*/
public static final String NAMESPACE = "http://WebXml.com.cn/";
/**
* 获取归属地信息URL
*/
public static String URL = "http://www.webxml.com.cn/webservices/MobileCodeWS.asmx";
/**
* 获得归属地方法名
*/
public static String GET_LOCATION_METHOD_NAME = "getMobileCodeInfo";
/**
* 获得归属地soapAction
*/
public static String GET_LOCATION_SOAP_ACTION = "http://WebXml.com.cn/getMobileCodeInfo";
/**
* 获取归属地返回数据key
*/
public static String GET_LOCATION_RESULT_NAME = "getMobileCodeInfoResult";
/**
* 获取字符串中的中文列表
* @param oriText
* @return
*/
public static List<String> GetChineseWord(String str_text)
{
List<String> chineseWordsList = new ArrayList<String>();
String reg="([\u4e00-\u9fa5]+)";
Matcher matcher = Pattern.compile(reg).matcher(str_text);
while(matcher.find()){
chineseWordsList.add(matcher.group());
}
return chineseWordsList;
}
}