xml 服务器获得后取得数值

前端之家收集整理的这篇文章主要介绍了xml 服务器获得后取得数值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. public static String sendPost(String url,String params) {
    //访问网络,url:地址  params:json信息即传入服务器信息,返回值result即ResponseInfo1 的xml
  2.    OutputStreamWriter  out = null;
  3.    BufferedReader in = null;
  4.    String result = "";
  5.    try {
  6.       HttpURLConnection conn = getHttpConn(url);
  7.       conn.setConnectTimeout(6000);
  8.       conn.setReadTimeout(5000);
  9.       conn.setRequestProperty("connection","Keep-Alive");
  10.       conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1; SV1)");
  11.       conn.setRequestProperty("Content-Type","application/json");
  12.       conn.setRequestMethod("POST");
  13.       conn.setDoOutput(true);
  14.       conn.setDoInput(true);
  15.       conn.setUseCaches(false);
  16.       out = new OutputStreamWriter(conn.getOutputStream());
  17.       out.write(params);
  18.       out.flush();
  19.       respCode = conn.getResponseCode();
  20.       Log.i(TAG,"conn.getResponseCode() ="+ respCode);
  21.       in = new BufferedReader(
  22.             new InputStreamReader(conn.getInputStream()));
  23.       String line;
  24.       while ((line = in.readLine()) != null) {
  25.          result += "\n" + line;
  26.       }
  27.    } catch (Exception e) {
  28.       result = "发送POST请求出现异常!" + e.toString();
  29.       e.printStackTrace();
  30.       Log.i(TAG,"e.printStackTrace ="+result);
  31.    } finally {
  32.       try {
  33.          if (out != null) {
  34.             out.close();
  35.          }
  36.          if (in != null) {
  37.             in.close();
  38.          }
  39.       } catch (IOException ex) {
  40.          ex.printStackTrace();
  41.       }
  42.    }
  43.    Log.i(TAG,"result ="+result);
  44.    return result;
  45. }

  1.  
  1.  
  1. //ResponseInfo1来源以上的sendPost函数
  1. String ResponseInfo1 = <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ats><code>99</code><message>版本不一致</message></ats>
  1. String ResponseInfo2  = ParseXML(ResponseInfo1,"<message>","</message>");  // = 版本不一致
    public static String ParseXML(String src,String key1,String key2){
  2.    if(src == null || key1 == null || key2 == null) {
  3.       return null;
  4.    }
  5.    
  6.    String result = "";
  7.    int begin = src.indexOf(key1);
  8.    if(begin != -1) {
  9.       result = src.substring(begin +key1.length());
  10.    }
  11.    int end = result.indexOf(key2);
  12.    if(end != -1) {
  13.       result = result.substring(0,end);
  14.    }
  15.    
  16.    return result;
  17. }
    打印
    ResponseInfo2  版本不一致

猜你在找的XML相关文章