httpclient 請(qǐng)求http數(shù)據(jù),json轉(zhuǎn)map的實(shí)例
實(shí)例如下:
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); //打印服務(wù)器返回的狀態(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);//轉(zhuǎn)換為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ī)?lái)的httpclient 請(qǐng)求http數(shù)據(jù),json轉(zhuǎn)map的實(shí)例全部?jī)?nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
淺談Java中BIO、NIO和AIO的區(qū)別和應(yīng)用場(chǎng)景
這篇文章主要介紹了Java中BIO、NIO和AIO的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04SpringCloud微服務(wù)架構(gòu)升級(jí)匯總
這篇文章主要介紹了SpringCloud微服務(wù)架構(gòu)升級(jí)匯總,它提倡將單一應(yīng)用程序劃分成一組小的服務(wù),服務(wù)之間互相協(xié)調(diào)、互相配合,為用戶提供最終價(jià)值,需要的朋友可以參考下2019-06-06如何使用SpringBootCondition更自由地定義條件化配置
這篇文章主要介紹了如何使用SpringBootCondition更自由地定義條件化配置,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下2021-04-04@FeignClient的使用和Spring?Boot的版本適配方式
這篇文章主要介紹了@FeignClient的使用和Spring?Boot的版本適配方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03springboot臨時(shí)文件存儲(chǔ)目錄配置方式
這篇文章主要介紹了springboot臨時(shí)文件存儲(chǔ)目錄配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot中Redis自動(dòng)配置的介紹、原理和使用詳解
這篇文章主要介紹了SpringBoot中Redis自動(dòng)配置的介紹、原理和使用詳解,Redis是一種高性能的鍵值對(duì)存儲(chǔ)數(shù)據(jù)庫(kù),它支持多種數(shù)據(jù)結(jié)構(gòu)和豐富的功能,如緩存、消息隊(duì)列、計(jì)數(shù)器、分布式鎖等,需要的朋友可以參考下2023-08-08SpringBoot整合Sa-Token實(shí)現(xiàn)登錄認(rèn)證的示例代碼
本文主要介紹了SpringBoot整合Sa-Token實(shí)現(xiàn)登錄認(rèn)證的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Java代碼性能優(yōu)化的35個(gè)方法總結(jié)
本篇文章主要介紹了Java代碼性能優(yōu)化的35個(gè)方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02