一个需求高手需要获取手机归属地,在网上看了好多代码试了好多都不行,都是到conn.setrequestmethod("post")就卡住了,也没有去研究具体是啥,后来看了获取手机归属地的webservice,地址:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
就自己写了,直接运用调用webservice的方法去实现,结果是可以行的;还显得更简单,,但不懂有没有弊端。
以下是代码
public class MobileService
{
//NameSpace命名空间
public final static String NameSpace = "http://WebXml.com.cn/";
//SoapOption命名空间+方法名
public final static String SoapOption = "http://WebXml.com.cn/getMobileCodeInfo";
//MethodName将要调用的方法名
public final static String MethodName = "getMobileCodeInfo";
//WebServiceURL WebService地址
public final static String WebServiceURL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
//定义一个SoapObject对象
public SoapObject request = null;
public String GetMobileAddress(String phoneNO)
{
request = new SoapObject(NameSpace, MethodName);
request.addProperty("mobileCode", phoneNO);//存储过程方法参数,参数名与webservice的参数名顺序都需要一致
request.addProperty("userID", "");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;//指定为调用.net
HttpTransportSE ht = new HttpTransportSE(WebServiceURL);
try
{
ht.call(SoapOption, envelope);
}
catch (Exception e)
{
e.printStackTrace();
}
//获取数据
try
{
Object result= (Object)envelope.getResponse();
String str=result.toString();
return str;//结果
}
catch (SoapFault e)
{
return null;
}
}
}
|
|