通过HTTP协议发送XML数据并调用
来源:互联网 发布:ios邪恶福利软件 编辑:程序博客网 时间:2024/05/22 04:32
1、发送xml数据给服务器 ,并非以请求参数方式发送:
http://192.168.1.10:8080/video/manage.do?xml=<xml>....</xml>
服务器端代码:VideoManageAction.java
public ActionForward getXML(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
InputStream inStream = request.getInputStream();
byte[] data = StreamTool.readInputStream(inStream);
String xml = new String(data, "UTF-8");
System.out.println(xml);
return mapping.findForward("result");
}
J2SE编写客户端向web服务器发生xml数据
public static void main(String[] args) throws Exception {
String path="http://192.168.1.100:8080/videoweb/video/manage.do?method=getXML";
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><persons><person id=\"23\"><name>中国</name><age>30</age></person></persons>";
byte[] data = xml.getBytes("UTF-8");//得到了xml的实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
conn.setRequestProperty("Content-Length",
String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
}
2、发送SOAP数据给服务器调用webservice,实现手机号归属地查询
访问网址www.webxml.com.cn ,浏览各种web service,打开手机号码归属地查询并进行解释。
实验步骤:
创建android工程MobileBelong,设置网络访问权限。
资源
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">手机号归属地查询</string>
<string name="mobile">手机号</string>
<string name="button">查询</string>
<string name="error">网络连接失败</string>
布局
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile" />
<EditText
android:id="@+id/mobile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="13472283596" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
在src目录下创建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></userID>
占位符
</soap12:Body>
</soap12:Envelope>
业务类:MobileService
注意访问目标地址是:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
可以有协议中得到。
package cn.class3g.service;
…
public class MobileService {
public static String getMobileAddress(String mobile)throws Exception {
InputStream inStream = MobileService.class.getClassLoader()
.getResourceAsStream("mobilesoap.xml");
byte[] data = StreamTool.readInputStream(inStream);
String xml = new String(data);
String soap = xml.replaceAll("\\$mobile", mobile);
/**
* 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile
* 而\为字符串中的特殊符号,所以用两个反斜杠,即"\\$"
*/
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
data = soap.getBytes();// 得到了xml的实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
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) {
InputStream responseStream = conn.getInputStream();
return parseXML(responseStream);
}
return null;
}
/**
* 解析返回xml数据
*
* @param responseStream
* @return
* @throws Exception
*/
private static String parseXML(InputStream responseStream)throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(responseStream, "UTF-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_TAG:
if ("getMobileCodeInfoResult".equals(parser.getName())) {
return parser.nextText();
}
break;
}
event = parser.next();
}
return null;
}
}
工具类StreamTool
package cn.class3g.utils;
…
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();
}
}
Activity类MobileBelongActivity
package cn.class3g.mobile;
…
public class MobileBelongActivity extends Activity {
private static final String TAG = "MainActivity";
private EditText mobileText;
private TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mobileText = (EditText) this.findViewById(R.id.mobile);
resultView = (TextView) this.findViewById(R.id.result);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mobile = mobileText.getText().toString();
try {
String address = MobileService.getMobileAddress(mobile);
resultView.setText(address);
} catch (Exception e) {
Log.e(TAG, e.toString());
Toast.makeText(MobileBelongActivity.this, R.string.error, 1).show();
}
}
});
}
}
- android 通过HTTP协议发送XML数据并调用
- 通过HTTP协议发送XML数据并调用
- android 通过HTTP协议发送XML数据并调用
- Android应用开发之通过HTTP协议发送XML数据并调用
- 通过Http协议发送XML数据
- Android--通过Http协议向web服务器发送XML数据和调用webService
- Android之通过HTTP协议向服务器发送XML数据
- 001-实现Unity3d通过Http协议 向PHP服务器发送和解析Json格式数据 并进行测试
- PB9.0 通过http协议以POST方式将XML发送到协议服务器
- 通过Http协议获取XML
- HTTP请求发送XML数据
- HTTP请求发送XML数据
- VB通过WINSOCK发送HTTP协议
- 怎么通过http向服务器发送数据并接受到返回的数据
- HTTP---Android 通过http协议数据交互
- C# 用HTTP协议发送、接收数据
- java 实现http协议发送接收数据
- 【转】Socket通过HTTP代理发送数据
- 第四篇博客:Gridview合并单元格(合并行和列)
- Rails宝典之第六式: Symbol to Proc
- Rails宝典之第七式: layout详解
- android控件26 SharedPreferences
- Normal Mapping整理资料
- 通过HTTP协议发送XML数据并调用
- android源码导入eclipse
- 瞎说呗
- Android: R cannot be resolved to a variable
- 如何从零开始学会 Ruby on Rails?
- 指针,其实很危险
- failed to lazily initialize a collection of role
- Linux命令详解–crontab
- Android平台向web应用提交信息