關於java實現http請求工具類示例
通過http rest請求返回數據
複製代碼 代碼如下:
import ;
import actory;
import ValuePair;
import ntProtocolException;
import Client;
import onseHandler;
import ncodedFormEntity;
import Get;
import Post;
import cResponseHandler;
import ultHttpClient;
import adSafeClientConnManager;
import ception;
import ageFormat;
import ;
import Unit;
/**
* 分裝一個http請求的.工具類
*
* @author 顧煒【guwei】 on 14-4-22.下午3:17
*/
public class HttpClientUtils {
private static final Log log = og(s);
/**
* 初始化HttpClient
*/
private static HttpClient httpClient = null;
/**
* 生產HttpClient實例
* 公開,靜態的工廠方法,需要使用時才去創建該單體
*
* @return
*/
public static HttpClient getHttpClient() {
if (httpClient == null) {
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
}
return httpClient;
}
/**
* POST方式調用
*
* @param url
* @param params 參數為NameValuePair鍵值對對象
* @return 響應字符串
* @throws pportedEncodingException
*/
public static String executeByPOST(String url, Listparams) {
HttpClient httpclient = getHttpClient();
HttpPost post = new HttpPost(url);
ResponseHandlerresponseHandler = new BasicResponseHandler();
String responseJson = null;
try {
if (params != null) {
ntity(new UrlEncodedFormEntity(params));
}
responseJson = ute(post, responseHandler);
("HttpClient POST請求結果:" + responseJson);
} catch (ClientProtocolException e) {
tStackTrace();
("HttpClient POST請求異常:" + essage());
} catch (IOException e) {
tStackTrace();
} finally {
onnectionManager()eExpiredConnections();
onnectionManager()eIdleConnections(30, NDS);
}
return responseJson;
}
/**
* Get方式請求
*
* @param url 帶參數佔位符的URL,例:http://****/User/user/{0}&email={1}
* @param params 參數值數組,需要與url中佔位符順序對應
* @return 響應字符串
* @throws pportedEncodingException
*/
public static String executeByGET(String url, Object[] params) {
HttpClient httpclient = getHttpClient();
String messages = at(url, params);
HttpGet get = new HttpGet(messages);
ResponseHandlerresponseHandler = new BasicResponseHandler();
String responseJson = null;
try {
responseJson = ute(get, responseHandler);
("HttpClient GET請求結果:" + responseJson);
} catch (ClientProtocolException e) {
tStackTrace();
("HttpClient GET請求異常:" + essage());
} catch (IOException e) {
tStackTrace();
("HttpClient GET請求異常:" + essage());
} finally {
onnectionManager()eExpiredConnections();
onnectionManager()eIdleConnections(30, NDS);
}
return responseJson;
}
/**
* @param url
* @return
*/
public static String executeByGET(String url) {
HttpClient httpclient = getHttpClient();
HttpGet get = new HttpGet(url);
ResponseHandlerresponseHandler = new BasicResponseHandler();
String responseJson = null;
try {
responseJson = ute(get, responseHandler);
("HttpClient GET請求結果:" + responseJson);
} catch (ClientProtocolException e) {
tStackTrace();
("HttpClient GET請求異常:" + essage());
} catch (IOException e) {
tStackTrace();
("HttpClient GET請求異常:" + essage());
} finally {
onnectionManager()eExpiredConnections();
onnectionManager()eIdleConnections(30, NDS);
}
return responseJson;
}
}
-
如何使用java
Java是一種可以撰寫跨平台應用程序的面向對象的`程序設計語言。以下是小編為大家搜索整理的如何使用java,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!...
-
2017年3月計算機二級Java基礎知識習題
學習是把知識能力思維方法等轉化為你的私有產權的重要手段,是“公有轉私”的重要途徑。以下是本站小編彙總的2017年3月計算機二級Java基礎知識習題,歡迎學習!1、結構化程序設計主要強調的是______。A、程序的規模B、程序的易讀性C、程序的執行效率D、程序的可移...
-
java中String和StringBuffer的區別
java中String和StringBuffer的區別String這個類是Java中使用得最頻繁的類之一,以下就是小編精心推薦java中String和StringBuffer的區別,希望對大家有幫助!看到這個講解的不錯,所以轉一下在java中有3個類來負責字符的操作。acter是進行單個字符操作的,ng對一串字符...
-
講述java讀取properties文件的方法
講述java讀取properties文件的方法1.不在項目中讀取:Propertiesproperties=newProperties();BufferedReaderread=newBufferedReader(newInputStreamReader(newFileInputStream("文件的.路徑"),"utf-8"));(read);propertiesroperty("那個文件的key")2.在項目中讀...