万能android调用webservice方法——参数类型不受限制

说明:只是个例子,扩展性、复用性不好,只是提出一个思路,返回的XML解析代码写的也很烂
聪明的你,拿来代码的时候,肯定能解决这些问题
===========================================
关键代码:

try {
// 发帖机原理,模拟浏览器
final String SERVER_URL = " http://10.40.15.11/Android/WebService.asmx " ; // 定义需要获取的内容来源地址

URL url
= new URL(SERVER_URL);
URLConnection con
= url.openConnection();
con.setDoOutput(
true );
con.setRequestProperty(
" Pragma: " , " no-cache " );
con.setRequestProperty(
" Cache-Control " , " no-cache " );
con.setRequestProperty(
" Content-Type " , " text/xml " );

OutputStreamWriter out
= new OutputStreamWriter(con
.getOutputStream());
// 控件取值
EditText eTextName = (EditText) findViewById(R.id.tbx_name);
EditText eTextAge
= (EditText) findViewById(R.id.tbx_age);
String xmlInfo
= " <?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><ShowEntity xmlns=\"http://tempuri.org/\"><person><Name> "
+ URLEncoder.encode(eTextName.getText().toString())
+ " </Name><Age> "
+ eTextAge.getText().toString()
+ " </Age></person></ShowEntity></soap12:Body></soap12:Envelope> " ;
// 发送
out.write( new String(xmlInfo.getBytes( " UTF-8 " )));
out.flush();
out.close();
// 取返回值
BufferedReader br = new BufferedReader( new InputStreamReader(con
.getInputStream()));
StringBuilder sBuilder
= new StringBuilder();
String line
= "" ;
for (line = br.readLine(); line != null ; line = br.readLine()) {
sBuilder.append(line);
}
// 解析XML
Pattern patternname = Pattern.compile( " <Name>.*?</Name> " );
Matcher matchername
= patternname.matcher(sBuilder.toString());
if (matchername.find()) {
String name
= matchername.group();
TextView lblname
= (TextView) findViewById(R.id.lbl_name);
lblname.setText(URLDecoder.decode(name.substring(name
.indexOf(
" > " ) + 1 , name.lastIndexOf( " < " ))));
}

Pattern patternage
= Pattern.compile( " <Age>.*?</Age> " );
Matcher matcherage
= patternage.matcher(sBuilder.toString());
if (matcherage.find()) {
String age
= matcherage.group();
TextView lblage
= (TextView) findViewById(R.id.lbl_age);
lblage.setText(age.substring(age.indexOf(
" > " ) + 1 , age
.lastIndexOf(
" < " )));
}

}
catch (Exception e) {
String str
= e.getMessage();
}

变量xmlInfo就是这里红线圈起来的地方

服务器端:c#代码

[WebMethod]
public Person ShowEntity(Person person)
{
return person;
}


public class Person
{
public string Name { get ; set ; }

public int Age { get ; set ; }
}

转载于:https://www.cnblogs.com/gzggyy/archive/2011/06/21/2086159.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值