1. 程式人生 > >呼叫圖靈機器人api2.0

呼叫圖靈機器人api2.0

json提取資料 https://blog.csdn.net/qq_37581708/article/details/72367803




通過post方法請求

package robot;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class HttpRobot {

	/**
	 * post請求,引數為json字串
	 * 
	 * @param url        請求地址
	 * @param jsonString json字串
	 * @return 響應
	 */
	public static String postJson(String url, String jsonString) {

		String result = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		CloseableHttpResponse response = null;

		try {
			post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
			response = httpClient.execute(post);
			if (response != null && response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				result = entityToString(entity);
				JSONObject jsonObject = JSONObject.fromObject(result); // 將字串轉化為JSONObject型別

				/*
				 * 分析 JSON的中的資料的格式,提取出來 System.out.println("jsonObject --->" + jsonObject);
				 * jsonObject ---> {"intent":{"actionName":"","code":7110,"intentName":""},
				 * "results":[{"groupType":0,"resultType":"text","values":{"text":"你的小可愛做錯了什麼嗎?"
				 * }}]}
				 * 
				 */
				JSONArray ja = jsonObject.getJSONArray("results");
				System.out.println("ja ---> " + ja);
				JSONObject jo = ja.getJSONObject(ja.size() - 1); // 提取出最後一個 text型別的資料-
				jsonObject = jo.getJSONObject("values");
				result = jsonObject.getString("text"); // 去除 value 中的 text資料
			}

			return result;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	private static String entityToString(HttpEntity entity) throws IOException {
		String result = null;
		if (entity != null) {
			long lenth = entity.getContentLength();
			if (lenth != -1 && lenth < 2048) {
				result = EntityUtils.toString(entity, "UTF-8");
			} else {
				InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
				CharArrayBuffer buffer = new CharArrayBuffer(2048);
				char[] tmp = new char[1024];
				int l;
				while ((l = reader1.read(tmp)) != -1) {
					buffer.append(tmp, 0, l);
				}
				result = buffer.toString();
			}
		}
		return result;
	}

	public static JSONObject transJosn(String text) { // 構造JSON請求引數
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("reqType", "0");

		JSONObject perception = new JSONObject();
		JSONObject inputText = new JSONObject();
		inputText.put("text", text);
		perception.put("inputText", inputText);
		jsonObject.put("perception", perception);

		JSONObject userInfo = new JSONObject();
		userInfo.put("apiKey", "2581f443bf364fd8a927fe87832e3d33");
		userInfo.put("userId", "fuchen");
		jsonObject.put("userInfo", userInfo);

		return jsonObject;
	}

}

呼叫(我這個是在微信公眾號中呼叫)

	JSONObject jsonObject = HttpRobot.transJosn(content); // content是文字訊息
	String js = HttpRobot.postJson(ROBOT_URL, jsonObject.toString());