电话号码查询web services: http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
将web services的soap协议提交给服务器,服务器根据soap协议调用相关的API接口,然后把信息返回给用户。
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
POST /WebServices/MobileCodeWS.asmx HTTP/1.1 Host: webservice.webxml.com.cn Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getMobileCodeInfo xmlns="http://WebXml.com.cn/"> <mobileCode>string</mobileCode> <userID>string</userID> </getMobileCodeInfo> </soap12:Body> </soap12:Envelope>
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"> <getMobileCodeInfoResult>string</getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap12:Body> </soap12:Envelope>
package com.haha.mobileServices;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.os.PatternMatcher;
import android.util.Xml;
import com.haha.tools.StreamTool;
public class HttpRequest {
public static String readSoapXML(InputStream inputStream ,String mobile)throws Throwable{//用于取得soap协议的xml文件用于向webServices请求
byte[] data=StreamTool.readInputStream(inputStream);
String soapString=new String(data);
Map<String, String> params=new HashMap<String, String>();
params.put("mobile", mobile);
String soapXml=replace(soapString, params);
return soapXml;
}
public static String replace(String xml,Map<String, String> params) throws Throwable{//替换电话号码的值用于查询,Map类型中key存放xml中需要被替换的,values存放替换上去的值
String resultString=xml;
if(params!=null&&!params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
String nameString="\\$"+entry.getKey();
Pattern pattern=Pattern.compile(nameString);//正则表达式匹配,用于替换
Matcher matcher=pattern.matcher(resultString);
if(matcher.find()){
resultString = matcher.replaceAll(entry.getValue());
}
}
}
return resultString;
}
public static String getMobileAddress(InputStream inputStream,String mobile)throws Throwable{ //连接网络设置发送请求(根据soap协议),获得web services信息
String soap=readSoapXML(inputStream, mobile);
byte[] data = soap.getBytes();
URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");/ /POST地址
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");//根据soap协议提供的
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200){
return parseResponseXML(conn.getInputStream());
}
return null;
}
private static String parseResponseXML(InputStream inputStream) throws XmlPullParserException, IOException {//解析xml获得需要的信息
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream, "UTF-8");
int eventType = parser.getEventType();//产生第一个事件
while(eventType!=XmlPullParser.END_DOCUMENT){//只要不是文档结束事件
switch (eventType) {
case XmlPullParser.START_TAG:
String name = parser.getName();//获取解析器当前指向的元素的名称
if("getMobileCodeInfoResult".equals(name)){
return parser.nextText();
}
break;
}
eventType = parser.next();
}
return null;
}
}
Activity-------------------android
public class MoblieAddressActivity extends Activity {
/** Called when the activity is first created. */
private TextView resultTextView;
private EditText editText;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText=(EditText)findViewById(R.id.editText);
String mobile=editText.getText().toString();
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");//把webservices的soap协议放在xml文件里进行读取
try {
String result=HttpRequest.getMobileAddress(inputStream, mobile);
resultTextView=(TextView)findViewById(R.id.text);
resultTextView.setText(result);
} catch (Throwable e) {
Toast.makeText(MoblieAddressActivity.this,R.string.error, 1).show();
}
}
});
}
}