Commit 96fb34f0920c26b40784ce2b385d301b444c7a39

Authored by 潘钊
1 parent 057b6f7d

commons-httpclient to httpclient 4.5.2

@@ -61,11 +61,11 @@ @@ -61,11 +61,11 @@
61 <artifactId>fastjson</artifactId> 61 <artifactId>fastjson</artifactId>
62 <version>1.2.4</version> 62 <version>1.2.4</version>
63 </dependency> 63 </dependency>
  64 +
64 <dependency> 65 <dependency>
65 - <groupId>commons-httpclient</groupId>  
66 - <artifactId>commons-httpclient</artifactId>  
67 - <version>3.1</version>  
68 - </dependency> 66 + <groupId>org.apache.httpcomponents</groupId>
  67 + <artifactId>httpclient</artifactId>
  68 + </dependency>
69 69
70 <dependency> 70 <dependency>
71 <groupId>commons-dbcp</groupId> 71 <groupId>commons-dbcp</groupId>
src/main/java/com/bsth/gpsdata/thread/GpsBufferRefreshThread.java
@@ -7,8 +7,11 @@ import java.util.HashSet; @@ -7,8 +7,11 @@ import java.util.HashSet;
7 import java.util.List; 7 import java.util.List;
8 import java.util.Set; 8 import java.util.Set;
9 9
10 -import org.apache.commons.httpclient.HttpClient;  
11 -import org.apache.commons.httpclient.methods.GetMethod; 10 +import org.apache.http.HttpEntity;
  11 +import org.apache.http.client.methods.CloseableHttpResponse;
  12 +import org.apache.http.client.methods.HttpGet;
  13 +import org.apache.http.impl.client.CloseableHttpClient;
  14 +import org.apache.http.impl.client.HttpClients;
12 import org.slf4j.Logger; 15 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory; 16 import org.slf4j.LoggerFactory;
14 import org.springframework.beans.factory.annotation.Autowired; 17 import org.springframework.beans.factory.annotation.Autowired;
@@ -81,7 +84,8 @@ public class GpsBufferRefreshThread extends Thread{ @@ -81,7 +84,8 @@ public class GpsBufferRefreshThread extends Thread{
81 84
82 logger.info("本次刷新GPS实时数据耗时:" + (System.currentTimeMillis() - t) + "毫秒"); 85 logger.info("本次刷新GPS实时数据耗时:" + (System.currentTimeMillis() - t) + "毫秒");
83 }catch(Exception e){ 86 }catch(Exception e){
84 - logger.error("", e); 87 + //logger.error("", e);
  88 + logger.error("加载gps数据失败");
85 } 89 }
86 } 90 }
87 91
@@ -95,33 +99,40 @@ public class GpsBufferRefreshThread extends Thread{ @@ -95,33 +99,40 @@ public class GpsBufferRefreshThread extends Thread{
95 public List<GpsRealData> getterRealGpsData() throws Exception{ 99 public List<GpsRealData> getterRealGpsData() throws Exception{
96 List<GpsRealData> list = new ArrayList<>(); 100 List<GpsRealData> list = new ArrayList<>();
97 101
98 - HttpClient client = new HttpClient();  
99 - GetMethod method = new GetMethod(url);  
100 - //要求服务器主动断开连接  
101 - method.setRequestHeader("Connection", "close"); 102 + CloseableHttpClient httpClient = null;
102 103
103 - int status = client.executeMethod(method);  
104 -  
105 - if(status == 200){  
106 - BufferedReader br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));  
107 -  
108 - StringBuffer stringBuffer = new StringBuffer();  
109 - String str= "";  
110 - while((str = br.readLine()) != null){  
111 - stringBuffer .append(str );  
112 - } 104 + try {
  105 + httpClient = HttpClients.createDefault();
  106 + HttpGet get = new HttpGet(url);
113 107
114 - JSONObject jsonObj = JSON.parseObject(stringBuffer.toString()); 108 + CloseableHttpResponse response = httpClient.execute(get);
115 109
116 - if(jsonObj != null)  
117 - list = JSON.parseArray(jsonObj.getString("data"), GpsRealData.class); 110 + try {
  111 + HttpEntity entity = response.getEntity();
  112 + if(null != entity){
  113 + //返回数据量比较大,建议以流的形式读取
  114 + BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
  115 + StringBuffer stringBuffer = new StringBuffer();
  116 + String str= "";
  117 + while((str = br.readLine()) != null){
  118 + stringBuffer .append(str );
  119 + }
  120 +
  121 + JSONObject jsonObj = JSON.parseObject(stringBuffer.toString());
  122 +
  123 + if(jsonObj != null)
  124 + list = JSON.parseArray(jsonObj.getString("data"), GpsRealData.class);
  125 + }
  126 + else
  127 + logger.error("result is null");
  128 + } finally {
  129 + response.close();
  130 + }
118 131
119 - //释放连接  
120 - method.releaseConnection(); 132 + } finally{
  133 + if(null != httpClient)
  134 + httpClient.close();
121 } 135 }
122 - else  
123 - logger.error("error status code: " + status);  
124 -  
125 return list; 136 return list;
126 } 137 }
127 } 138 }