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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM