Browse Source

长文召回

wangyunpeng 11 months ago
parent
commit
14c1f03679

+ 15 - 10
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/AIGCRemoteService.java

@@ -6,15 +6,18 @@ import com.tzld.longarticle.recommend.server.common.HttpPoolFactory;
 import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.service.recall.RecallParam;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.http.HttpEntity;
 import org.apache.http.StatusLine;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * @author dyp
@@ -38,18 +41,20 @@ public class AIGCRemoteService {
         try {
             HttpPost httpPost = new HttpPost(aigcGetAllContentUrl);
             StringEntity stringEntity = new StringEntity(bodyParam.toJSONString());
-            stringEntity.setContentType("application/json");
-            stringEntity.setContentEncoding("UTF-8");
+            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
             httpPost.setEntity(stringEntity);
-            CloseableHttpResponse response22 = client.execute(httpPost);
-            StatusLine statusLine = response22.getStatusLine();
+            CloseableHttpResponse response = client.execute(httpPost);
+            StatusLine statusLine = response.getStatusLine();
             if (statusLine.getStatusCode() == 200) {
-                String responseBody = response22.getEntity().getContent().toString();
-                log.info("getAllContent AIGC返回的数据:{}", responseBody);
-                JSONObject jsonObject = JSONObject.parseObject(responseBody);
-                if (jsonObject.getInteger("code") == 0) {
-                    JSONArray data = jsonObject.getJSONArray("data");
-                    result.addAll(JSONArray.parseArray(data.toJSONString(), Content.class));
+                HttpEntity responseEntity = response.getEntity();
+                if (Objects.nonNull(responseEntity)) {
+                    String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
+                    log.info("getAllContent AIGC返回的数据:{}", responseBody);
+                    JSONObject jsonObject = JSONObject.parseObject(responseBody);
+                    if (jsonObject.getInteger("code") == 0) {
+                        JSONArray data = jsonObject.getJSONArray("data");
+                        result.addAll(JSONArray.parseArray(data.toJSONString(), Content.class));
+                    }
                 }
             }
         } catch (Exception e) {