/** 
    * @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;

发布评论
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Java 读取某个目录下所有文件、文件夹知识解答
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。