首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

经过HTTP协议发送XML数据并调用webservic

2012-09-25 
通过HTTP协议发送XML数据并调用webservic1.mobilesoap.xml?xml version1.0 encodingutf-8 ? - so

通过HTTP协议发送XML数据并调用webservic

1.mobilesoap.xml

  <?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>$mobile</mobileCode>   <userID />   </getMobileCodeInfo>  </soap12:Body>  </soap12:Envelope>

?2.StreamTool.java

package cn.itcast.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTool {/** * 从输入流读取数据 * @param inStream * @return * @throws Exception */public static byte[] readInputStream(InputStream inStream) throws Exception{ByteArrayOutputStream outSteam = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while( (len = inStream.read(buffer)) !=-1 ){outSteam.write(buffer, 0, len);}outSteam.close();inStream.close();return outSteam.toByteArray();}}

?

3.MobileInfoService

package cn.itcast.service;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.regex.Matcher;import java.util.regex.Pattern;import org.xmlpull.v1.XmlPullParser;import android.util.Xml;import cn.itcast.utils.StreamTool;public class MobileInfoService {private static String readSoapFile(InputStream inStream, String mobile) throws Exception{byte[] data = StreamTool.readInputStream(inStream);String soapxml = new String(data);Map<String, String> params = new HashMap<String, String>();params.put("mobile", mobile);return replace(soapxml, params);}public static String replace(String xml, Map<String, String> params)throws Exception{String result = xml;if(params!=null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){String name = "\\$"+ entry.getKey();Pattern pattern = Pattern.compile(name);Matcher matcher = pattern.matcher(result);if(matcher.find()){result = matcher.replaceAll(entry.getValue());}}}return result;}public static String getMobileAddress(InputStream inStream, String mobile)throws Exception{String soap = readSoapFile(inStream, mobile);byte[] data = soap.getBytes();URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");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");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 inStream) throws Exception{XmlPullParser parser = Xml.newPullParser();parser.setInput(inStream, "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;}}

?

4.MainActivity.java

package cn.itcast.mobile.address;import java.io.InputStream;import cn.itcast.service.MobileInfoService;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    private EditText mobileText;    private TextView addressView;    private static final String TAG = "MainActivity";        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                mobileText = (EditText)this.findViewById(R.id.mobile);        addressView = (TextView)this.findViewById(R.id.address);        Button button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String mobile = mobileText.getText().toString();InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");try {addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile));} catch (Exception e) {Log.e(TAG, e.toString());Toast.makeText(MainActivity.this, "查询失败", 1).show();}}});    }}

?5.AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8" ?> - <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.mobile.address" android:versionCode="1" android:versionName="1.0">- <application android:icon="@drawable/icon" android:label="@string/app_name">- <activity android:name=".MainActivity" android:label="@string/app_name">- <intent-filter>  <action android:name="android.intent.action.MAIN" />   <category android:name="android.intent.category.LAUNCHER" />   </intent-filter>  </activity>  </application>  <uses-sdk android:minSdkVersion="8" /> - <!--  访问网络的权限   -->   <uses-permission android:name="android.permission.INTERNET" />   </manifest>

?

热点排行