인 터 페 이 스 를 조정 하여 데 이 터 를 얻 는 몇 가지 방식

13180 단어 자바
package com.cuslink.visual.controller;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.cuslink.visual.pojo.DataParam;
public class GroundData { @GetMapping ("/topicList") @ResponseBody public Map HttpGetTopList() throws IOException { BufferedReader in = null; String urlNameString = “http://10.97.128.167/search/client/search/topic”; try { URL realUrl = new URL(urlNameString); URLConnection connection = realUrl.openConnection(); connection.setRequestProperty(“accept”, “/”); connection.setRequestProperty(“Content-Type”, “application/json;charset=UTF-8”); connection.setRequestProperty(“connection”, “Keep-Alive”); connection.setRequestProperty(“user-agent”, “Mozilla/4.0 (compatible; MSIE 5.0; Windows NT;DigExt)”); connection.setRequestProperty(“whoami”, “cci-haiguan”); connection.connect(); InputStream inputStream = connection.getInputStream();
            BufferedReader isr = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            int len = 0;
            byte[] rs = new byte[1024];
            String result = "";
            String s = "";
            while ((s = isr.readLine()) != null) {
                result += s;
            }
            JSONObject fromObject = JSONObject.parseObject(result);
            JSONArray h986Array = JSONArray.parseArray(fromObject.getString("data"));

            ArrayList arrayList = new ArrayList();
            for (Object object : h986Array) {
                Map map = JSONObject.parseObject(JSONObject.toJSONString(object), Map.class);
                String object3 = map.get("children").toString();
                JSONArray parseArray = JSONArray.parseArray(object3);
                for (Object object4 : parseArray) {
                    Map map1 = JSONObject.parseObject(JSONObject.toJSONString(object4), Map.class);
                    String objId = (String) map1.get("objId");
                    String primaryTableId = (String) map1.get("primaryTableId");
                    Object clsName = map1.get("clsName");
                    Map hashMap = new HashMap<>();
                    hashMap.put("topic", objId);
                    hashMap.put("table" , primaryTableId);
                    hashMap.put("clsName", clsName);
                    arrayList.add(hashMap);

                }

            }
            Map stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("row",arrayList);
            return stringObjectHashMap;
        } catch (Exception e) {
            System.out.println("  GET      !" + e);
            e.printStackTrace();
        }
        //   finally       
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    @PostMapping("/yunqin")
    @ResponseBody
    public Map yunqinData(@RequestBody Map params) {
        String ip="http://10.97.128.167";
        String url=ip+"/search/client/search";
        String key=params.get("key");
        String topic=params.get("topic");
        String table=params.get("table");
        int from = Integer.parseInt(params.get("from"));
        int size=Integer.parseInt(params.get("size"));
        String json = "{\r
" + " \"key\":\""+key+"\",\r
" + " \"topic\":\""+topic+"\",\r
" + " \"table\":\""+table+"\",\r
" + " \"from\":"+from+",\r
" + " \"size\":"+size+",\r
" + " \"isAcc\":false\r
" + "}"; String datatotal = httpPost(url,json); JSONObject parseObject = JSON.parseObject(datatotal); String datastring = parseObject.getString("data"); JSONObject data = JSON.parseObject(datastring); Map map1 = JSONObject.parseObject(JSONObject.toJSONString(data), Map.class); Object total = map1.get("total"); Object from1= map1.get("from"); Object size1 = map1.get("size"); String data1 = map1.get("data").toString(); JSONArray objects = JSONArray.parseArray(data1); ArrayList datalist = new ArrayList<>(); for (Object object : objects) { Map map2 = JSONObject.parseObject(JSONObject.toJSONString(object), Map.class); String href1 = (String) map2.get("href"); String href= ip+href1.replaceFirst("search", "web"); Object title = map2.get("title"); Object body = map2.get("body"); HashMap stringObjectHashMap = new HashMap<>(); stringObjectHashMap.put("href",href); stringObjectHashMap.put("title",title); stringObjectHashMap.put("body",body); datalist.add(stringObjectHashMap); } Map map2 =new HashMap(); map2.put("total",total); map2.put("from",from1); map2.put("size",size1); map2.put("data",datalist); return map2; } @PostMapping("/baidu") @ResponseBody public Map baiduData(@RequestBody Map maps) { String key = (String) maps.get("key"); int from = (int) maps.get("from"); int size = (int) maps.get("size"); String pageNo = String.valueOf(from); String pageSize = String.valueOf(size); Map map=new HashMap<>(); map.put("X-App-Id", "aies"); map.put("X-Access-Token", "aies"); map.put("q", key); map.put("pageNo", pageNo); map.put("pageSize", pageSize); String url="http://10.99.87.81:8080/search/site/search/aies_weba?"; String s = httpPost(url, map); Map map1 = JSONObject.parseObject(s, Map.class); return map1; } public static String httpPost(String urlStr,Map params){ StringBuffer data = new StringBuffer(); try { URL connect = new URL(urlStr); String XAppId= params.get("X-App-Id"); String XAccessToken= params.get("X-Access-Token"); String q = params.get("q"); String pageSize =params.get("pageSize"); String pageNo = params.get("pageNo"); String fq = params.get("fq"); String sort = params.get("sort"); String fl = params.get("fl"); Map hashMap = new HashMap<>(); String pageSize1="2"; hashMap.put("q", q); hashMap.put("pageSize",pageSize); hashMap.put("pageNo", pageNo); hashMap.put("qf", "title content_t"); hashMap.put("hl.fl","title,content_t"); String font=""; hashMap.put("hl.pre",font); hashMap.put("hl.post",""); hashMap.put("sort","score desc"); if(fq!=null&&fq.length()!=0) { hashMap.put("fq", fq); } if(sort!=null&&sort.length()!=0) { hashMap.put("sort", sort); } if(fl!=null&&fl.length()!=0) { hashMap.put("fl", fl); } HttpURLConnection connection = (HttpURLConnection)connect.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false);//post connection.setInstanceFollowRedirects(true); connection.setRequestMethod("POST");// connection.setRequestProperty("Accept", "application/json");// connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// connection.setRequestProperty("X-App-Id", XAppId);// connection.setRequestProperty("X-Access-Token", XAccessToken);// OutputStreamWriter paramout = new OutputStreamWriter(connection.getOutputStream(),"UTF-8"); String paramsStr = ""; // Post for(String param : hashMap.keySet()){ paramsStr += "&" + param + "=" + hashMap.get(param); } if(!paramsStr.isEmpty()){ paramsStr = paramsStr.substring(1); } paramout.write(paramsStr); paramout.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "UTF-8")); String line; while ((line = reader.readLine()) != null) { data.append(line); } paramout.close(); reader.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return data.toString(); } public static String httpPost(String urlStr,String json){ StringBuffer data = new StringBuffer(); try { URL connect = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection)connect.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false);//post connection.setInstanceFollowRedirects(true); connection.setRequestMethod("POST");// connection.setRequestProperty("Content-Type", "application/json");//

//connection.setRequestProperty(“whoami”, “cci-haiguan”); OutputStreamWriter paramout = new OutputStreamWriter(connection.getOutputStream(),“UTF-8”); paramout.write(json); paramout.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), “UTF-8”)); String line; while ((line = reader.readLine()) != null) { data.append(line); } paramout.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } return data.toString(); }
    public static void main(String[] args) {
		DataParam dataParam = new DataParam();
		dataParam.setAPP_ID("924169EA-8505-4B19-9177-CB437E6DE5EE");
		dataParam.setMESSAGE_CODE("Z001");
		net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(dataParam);
		String jsonStr = jsonObject.toString();
		String url = "http://10.200.5.10/ExtApi/Api/Data/Query";
		String data = httpPost(url,jsonStr);
		String datas = "";
		
		JSONObject json = JSONObject.parseObject(data);
		datas = json.getString("Datas");

//JSSONarry jon = JSSONarray. parseArray (datas);///json 배열 목록 목록 목록 목록 목록 목록 목록 목록 목록 목록 목록 목록 목록 목록 Obj = (List >) JSNAarry. parse (datas); for (Map mapList: listObj) {for (Map. Entry ent리 ent리: mapList. entySet()) {System. out. print. print("키:"키: "+ enty. getKey() +"값: "+ enty. get값 값 값 ());}}/////for (int =///////JSSObject = jon... getJSOSObject.. getgetObObObject........ get호스트 호스트 호스트 호스트 호스트 호스트 호스트 호스트 호스트 호스트 호스트 호스트 호스트 호스트 를 (i);//Map xxMap = don.parseObject(don);//}
//Map jsonToMap = JSONObject.parseObject(datas);//Iterator> it = jsonToMap.entrySet().iterator();//while(it.hasNext()) {//Entry entry = it.next();//System.out.println(“key:”+entry.getKey()+“value:”+entry.getValue());//} }
}

좋은 웹페이지 즐겨찾기