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;
  • 相关阅读:
    数据后台查询分页条件查询数据
    避免js缓存
    jquery点击按钮
    网页内容打印
    数据表的导出
    C#实现字符串按多个字符采用Split方法分割
    JQuery
    AJAX
    JSON
    BOM
  • 原文地址:https://www.cnblogs.com/shaosks/p/9628153.html
Copyright © 2011-2022 走看看