• 万能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;
    }


    publicclass Person
    {
    publicstring Name { get; set; }

    publicint Age { get; set; }
    }
  • 相关阅读:
    mybatis_7分页查询
    mybatis_6日志工厂
    mybatis_5解决属性名和字段名不一致的问题(resultMap)
    mybatis_4配置解析
    mybatis_3CRUD操作
    ARM C函数调用堆栈入栈顺序
    syscall SYSCALL_DEFINE*()实现
    ko kallsyms
    elf文件结构解析
    ko module加载flow
  • 原文地址:https://www.cnblogs.com/gzggyy/p/2086159.html
Copyright © 2020-2023  润新知