zoukankan      html  css  js  c++  java
  • java 利用HttpURLConnection方式获取restful格式的服务数据

      

    /**
        * @Author:
        * @Description:利用HttpURLConnection方式获取restful格式的服务数据
        * @Date:
        */
        private static List<Object> getjsonlist(String targetURL){
            List<Object> result_list = new ArrayList<Object>();
         
            try {
                URL restServiceURL = new URL(targetURL);
                HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
                httpConnection.setRequestMethod("GET");
                httpConnection.setRequestProperty("Accept", "application/json");
                if (httpConnection.getResponseCode() != 200) {
                    throw new RuntimeException("HTTP GET Request Failed with Error code : "
                            + httpConnection.getResponseCode());
                }
    
                BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                        (httpConnection.getInputStream())));
                String output;
                String result= "";
                while ((output = responseBuffer.readLine()) != null) {
                    result=output;
                }
                httpConnection.disconnect();
    
                //输出结果内容格式如下:
                //<string xmlns="http://od.xxxxa.cn/">{"Result":["91443d34-524f-4fdd-b052-cca52777f434","29b601af-f534-46c7-b272-5801469aaf1c"],
                // "ExeResult":true,"ErrorMessage":null,"Message":null,"ResponseTime":"2018-09-11 14:19:24.756",
                // "RequestGuid":"c887f1b4-3238-4ed7-a5c2-305fff97caa8"}</string>
                int firstIndex = result.indexOf("{");
                int lastIndex =result.lastIndexOf("}");
                result = result.substring(firstIndex,lastIndex+1);
                result_list= CommonMethod.getJsonList(result);
                //内容为:
                //{"Result":["91443d34-524f-4fdd-b052-cca52777f434","29b601af-f534-46c7-b272-5801469aaf1c"],
                // "ExeResult":true,"ErrorMessage":null,"Message":null,"ResponseTime":"2018-09-11 14:19:24.756",
                // "RequestGuid":"c887f1b4-3238-4ed7-a5c2-305fff97caa8"}
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result_list;
        }
     /**
         * @Author:sks
         * @Description:根据Json串获取对应的列表,Json字符串格式如下:
         * {"Result":["9cc13ba8-9f4c-40f9-b019-4b885b8f4e7a","8357616f-0cd6-4a8d-838b-264f7b26f2e9"],
         * "ExeResult":true,"ErrorMessage":null,"Message":null,"ResponseTime":"2018-09-11 14:37:04.335",
         * "RequestGuid":"cac0bbb6-f7a4-4613-9cee-2a90af7cb7af"}
         * 要获取ExeResult值为true的结果:["9cc13ba8-9f4c-40f9-b019-4b885b8f4e7a","8357616f-0cd6-4a8d-838b-264f7b26f2e9"]
         * @Date:2018-09-11
         */
        public static List<Object> getJsonList(String json){
            List<Object>  list =null;
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null && jsonObj.get("ExeResult").toString() == "true") {
                    JSONArray objar = new JSONArray(jsonObj.get("Result").toString());
                    list = objar.toList();
                }
            }
            catch (JSONException ex){
                ex.printStackTrace();
            }
            return list;
        }
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.io.IOException;
  • 相关阅读:
    更新 箫心病毒分析专家2006 build 5.23(C#2.0)
    SOA也是一种设计模式
    SOA在美国和中国的差异
    单服务器配置部署Visual Studio 2005 Team Foundation Server步骤
    无废话ExtJs 入门教程十六[页面布局:Layout]
    无废话ExtJs 入门教程七[登陆窗体Demo:Login]
    无废话ExtJs 入门教程十八[树:TreePanel]
    无废话ExtJs 入门教程十一[下拉列表:Combobox]
    无废话ExtJs 入门教程十[单选组:RadioGroup、复选组:CheckBoxGroup]
    无废话ExtJs 入门教程八[脚本调试Firefox:firebug]
  • 原文地址:https://www.cnblogs.com/shaosks/p/9628153.html
Copyright © 2011-2022 走看看