获取token类 AuthService.java
package com.example.myapp.util;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class AuthService {
public static String access_token = "";
public static String getAuth() {
String clientId = "此处填写官网获取的 API Key";
String clientSecret = "此处填写官网获取的 Secret Key";
return getAuth(clientId, clientSecret);
}
public static String getAuth(String ak, String sk) {
if (!access_token.equals("")) {
return access_token;
}
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
String getAccessTokenUrl = authHost
+ "grant_type=client_credentials"
+ "&client_id=" + ak
+ "&client_secret=" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
JSONObject jsonObject = new JSONObject(result);
access_token = jsonObject.getString("access_token");
return access_token;
} catch (Exception e) {
System.err.printf("获取token失败!");
e.printStackTrace(System.err);
}
return null;
}
}
获取Unit服务 UnitService.java
package com.example.myapp.util;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class UnitService {
public static String utterance(String query,String session_id,String log_id) {
String talkUrl = "https://aip.baidubce.com/rpc/2.0/unit/service/chat";
Map<String, Object> map = new HashMap<>();
Map<String, Object> mapRequest = new HashMap<>();
Map<String, Object> mapQueryInfo = new HashMap<>();
Map<String, Object> mapClientSession = new HashMap<>();
List<String> asrCandidatesList = new ArrayList<>();
List<String> candidateOptionsList = new ArrayList<>();
List<String> skillList = new ArrayList<>();
skillList.add("1094636");
skillList.add("1094633");
skillList.add("1094635");
skillList.add("1094634");
map.put("request", mapRequest);
map.put("skill_ids", skillList);
map.put("service_id", "S51502");
map.put("version", "2.0");
map.put("session_id", session_id);
map.put("log_id", log_id);
mapRequest.put("bernard_level", 1);
mapRequest.put("query", query);
mapRequest.put("query_info", mapQueryInfo);
mapRequest.put("updates", "");
mapRequest.put("user_id", "UNIT_WEB_37819");
mapQueryInfo.put("asr_candidates", asrCandidatesList);
mapQueryInfo.put("source", "KEYBOARD");
mapQueryInfo.put("type", "TEXT");
String clientSession = "";
mapClientSession.put("client_results", "");
mapClientSession.put("candidate_options", candidateOptionsList);
mapRequest.put("client_session", clientSession);
try {
String params = new Gson().toJson(map);
String accessToken = AuthService.getAuth();
String result = post(talkUrl, accessToken, params);
System.out.println(" result: " + result);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String post(String url, String accessToken, String param) {
OutputStreamWriter out = null;
BufferedReader reader = null;
String response = "";
try {
URL httpUrl = null;
httpUrl = new URL(url + "?access_token=" + accessToken);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "keep-alive");
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
out = new OutputStreamWriter(
conn.getOutputStream());
out.write(param);
out.flush();
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
response += lines;
}
reader.close();
conn.disconnect();
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
finally {
try {
if (out != null) {
out.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return response;
}
}
字符解析工具 StringUtils.java
package com.example.myapp.util;
public class StringUtils {
public static boolean isEmpty(String str){
if(str == null || str.length()<=0){
return true;
}else{
return false;
}
}
}
AppConfig.java
package com.example.myapp.util;
public class AppConfig {
public static final String BASE_URl = "http://192.168.31.32:8080/renren-fast";
}
|