一個需求需要擷取手機歸屬地,在網上看了好多代碼試了好多都不行,都是到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;
}
}
}