此处使用的是springboot
- 单条数据
服务端:
@RequestMapping(value="/sentimentAnalysisPost",method = RequestMethod.POST) public String getSentimentAnalysisResultPost1(@RequestBody String text){ if(text != null){ String context = Dataprocessing.WordSegmentation(text); int a = SentimentAnalysisModel.getSentimentAnalysisModel().sentimentRnn(context); return String.valueOf(a); } return ""; }
客户端:
public static String PostRequest(String URL,String context) { String jsonString=""; try { //创建连接 URL url = new URL(URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); //设置请求方法 connection.setRequestProperty("Charset", "UTF-8"); //设置请求编码 connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/application/x-www-form-urlencoded"); connection.connect(); //POST请求 PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8")); out.write(context); out.flush(); out.close(); // 读取响应 if (connection.getResponseCode()==200) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; StringBuilder sb = new StringBuilder(); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } jsonString=sb.toString(); reader.close(); }//返回值为200输出正确的响应信息 if (connection.getResponseCode()==400) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream())); String lines; StringBuilder sb = new StringBuilder(); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } jsonString=sb.toString(); reader.close(); }//返回值错误,输出错误的返回信息 // 断开连接 connection.disconnect(); } catch (IOException e) { e.printStackTrace(); } return jsonString; } public static void main(String[] args){ System.println.out(PostRequest("http://127.0.0.1:8080/sentimentAnalysisPost","你好")); }
-
批量数据
服务端:这里可以填List也可以填Map,如果是List就是直接将Map<String, String> text 换成List<String> text
@RequestMapping(value = "/sentimentAnalysisBatchPost", method = RequestMethod.POST) @ResponseBody public Map<String,String> getSentimentAnalysisResultPost2(@RequestBody Map<String,String> text) { Map<String,String> map = new HashMap<>(); if (text.size() != 0) { for (String str : text.keySet()) { String context = Dataprocessing.WordSegmentation(text.get(str)); int a = SentimentAnalysisModel.getSentimentAnalysisModel().sentimentRnn(context); map.put(str,String.valueOf(a)); } } return map; }
客户端:
public static String PostRequest(String URL,String title) { String jsonString=""; try { //创建连接 URL url = new URL(URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); //设置请求方法 connection.setRequestProperty("Charset", "UTF-8"); //设置请求编码 connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/json"); connection.connect(); //POST请求 PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8")); out.write(title); out.flush(); out.close(); // 读取响应 if (connection.getResponseCode()==200) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; StringBuilder sb = new StringBuilder(); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } jsonString=sb.toString(); reader.close(); }//返回值为200输出正确的响应信息 if (connection.getResponseCode()==400) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream())); String lines; StringBuilder sb = new StringBuilder(); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } jsonString=sb.toString(); reader.close(); }//返回值错误,输出错误的返回信息 // 断开连接 connection.disconnect(); } catch (IOException e) { e.printStackTrace(); } return jsonString; } public static void main(String[] args){ // String body = new JSONArray(new String[]{"你好","你不好"}).toString(); Map<String,String> map = new HashMap<>(); map.put("id1","你好"); map.put("id2","你不好"); JSONObject jsonObject = new JSONObject(map); String body2 = jsonObject.toString(); System.out.println(PostRequest("http://127.0.0.1:8080/sentimentAnalysisBatchPost",body2)); }