|
@@ -16,9 +16,7 @@ import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
@@ -48,32 +46,36 @@ public class ArticleListRemoteService {
|
|
|
bodyParam.put("top_n", 10000);
|
|
|
bodyParam.put("use_max_time", true);
|
|
|
bodyParam.put("use_min_time", true);
|
|
|
- try {
|
|
|
- HttpPost httpPost = new HttpPost(articleListUrl);
|
|
|
- StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
|
|
|
- httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
- httpPost.setEntity(stringEntity);
|
|
|
- CloseableHttpResponse response = client.execute(httpPost);
|
|
|
- StatusLine statusLine = response.getStatusLine();
|
|
|
- if (statusLine.getStatusCode() == 200) {
|
|
|
- HttpEntity responseEntity = response.getEntity();
|
|
|
- if (Objects.nonNull(responseEntity)) {
|
|
|
- String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
|
|
|
- log.info("articleList 返回的数据:{}", responseBody);
|
|
|
- JSONObject articleListResponse = JSONObject.parseObject(responseBody);
|
|
|
- if (Objects.nonNull(articleListResponse)) {
|
|
|
- JSONArray articleList = articleListResponse.getJSONArray("article_list");
|
|
|
- if (CollectionUtils.isNotEmpty(articleList)) {
|
|
|
- return articleList.toJavaList(Article.class);
|
|
|
+ int retry = 0;
|
|
|
+ while (retry < 3) {
|
|
|
+ retry++;
|
|
|
+ try {
|
|
|
+ HttpPost httpPost = new HttpPost(articleListUrl);
|
|
|
+ StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ httpPost.setEntity(stringEntity);
|
|
|
+ CloseableHttpResponse response = client.execute(httpPost);
|
|
|
+ StatusLine statusLine = response.getStatusLine();
|
|
|
+ if (statusLine.getStatusCode() == 200) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ if (Objects.nonNull(responseEntity)) {
|
|
|
+ String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
|
|
|
+ log.info("articleList 返回的数据:{}", responseBody);
|
|
|
+ JSONObject articleListResponse = JSONObject.parseObject(responseBody);
|
|
|
+ if (Objects.nonNull(articleListResponse)) {
|
|
|
+ JSONArray articleList = articleListResponse.getJSONArray("article_list");
|
|
|
+ if (CollectionUtils.isNotEmpty(articleList)) {
|
|
|
+ return articleList.toJavaList(Article.class);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("articleList error", e);
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("articleList error", e);
|
|
|
}
|
|
|
log.info("articleList耗时:{}", System.currentTimeMillis() - start);
|
|
|
- return null;
|
|
|
+ return Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
}
|