httpclient 請求http數(shù)據(jù),json轉map的實例
更新時間:2016年12月31日 10:34:47 投稿:jingxian
下面小編就為大家?guī)硪黄猦ttpclient 請求http數(shù)據(jù),json轉map的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實例如下:
package com.idc.dataAudit.dataResource; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; public class RequestHttpManage { public Map<Integer,String> RequesthttpInfo(String url) { String resultStr = null; Map<Integer,String> mapResult = new HashMap<Integer,String>(); try{ HttpClient client = new HttpClient(); HttpMethod method=new GetMethod(url); //使用POST方法 //HttpMethod method = new PostMethod("http://java.sun.com"); client.executeMethod(method); //打印服務器返回的狀態(tài) //System.out.println(method.getProtocolVersion()); //System.out.println(method.getStatusLine().getStatusCode()); //System.out.println(method.getStatusLine().getReasonPhrase()); // System.out.println(method.getStatusLine().toString()); // System.out.println(method.getStatusLine()); //打印返回的信息 // System.out.println(method.getResponseBodyAsString()); mapResult.put(method.getStatusLine().getStatusCode(), method.getResponseBodyAsString()); //釋放連接 method.releaseConnection(); }catch(Exception e){ e.printStackTrace(); } System.out.println(mapResult); return mapResult; } public Map<String,List<String>> GetMapFromJson(String jsonStr){ Map<String,List<String>> mapResult = new HashMap<String,List<String>>(); try{ JSONObject result = JSONObject.fromObject(jsonStr);//轉換為JSONObject JSONArray nameList = result.getJSONArray("result");//獲取JSONArray int length = nameList.size(); for(int i=0;i<length;i++){ JSONObject tempObject = nameList.getJSONObject(i); Iterator iter = tempObject.keys(); String key = (String)iter.next(); String keyId = tempObject .getString(key); List<String> objList = new ArrayList<String>(); for (; iter.hasNext();) { String keyLocal = (String)iter.next(); // System.out.println(key+"="+tempObject .getString(keyLocal)); objList.add(tempObject .getString(keyLocal)); } mapResult.put(keyId, objList); } }catch(Exception e){ e.printStackTrace(); } System.out.println(mapResult); return mapResult; } }
以上就是小編為大家?guī)淼膆ttpclient 請求http數(shù)據(jù),json轉map的實例全部內(nèi)容了,希望大家多多支持腳本之家~
相關文章
淺談Java中BIO、NIO和AIO的區(qū)別和應用場景
這篇文章主要介紹了Java中BIO、NIO和AIO的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04如何使用SpringBootCondition更自由地定義條件化配置
這篇文章主要介紹了如何使用SpringBootCondition更自由地定義條件化配置,幫助大家更好的理解和學習使用springboot框架,感興趣的朋友可以了解下2021-04-04@FeignClient的使用和Spring?Boot的版本適配方式
這篇文章主要介紹了@FeignClient的使用和Spring?Boot的版本適配方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot中Redis自動配置的介紹、原理和使用詳解
這篇文章主要介紹了SpringBoot中Redis自動配置的介紹、原理和使用詳解,Redis是一種高性能的鍵值對存儲數(shù)據(jù)庫,它支持多種數(shù)據(jù)結構和豐富的功能,如緩存、消息隊列、計數(shù)器、分布式鎖等,需要的朋友可以參考下2023-08-08SpringBoot整合Sa-Token實現(xiàn)登錄認證的示例代碼
本文主要介紹了SpringBoot整合Sa-Token實現(xiàn)登錄認證的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01