概述:
由于采用语音和文本混合,所以不能通过简单post请求,需要multipart/form-data上传形式
multipart/form-data 类似于分块,每一个块可以代表不同的内容,为每一块命不同的名称加以区分,每块都有分割线。
详情可以参考:点击打开链接
- application/x-www-form-urlencoded(默认值)
- multipart/form-data
1、maven的pom.xml文件,必要的就是httpmime jar包,上传视频或者音频处理jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jack</groupId>
<artifactId>qianxun</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>jboss</id>
<name>JBoss Repository</name>
<url>http://repository.jboss.com/maven2/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.26</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
2、测试代码
@Test
public void ad() throws ClientProtocolException, IOException{
Map map = new HashMap();
//其它参数参考API
map.put("ak", "填入图灵给的appkey");
map.put("uid", "填入ase加密的uid");
map.put("asr", new Integer(1));
map.put("tts", new Integer(1));
map.put("flag", new Integer(3));
String url = "http://smartdevice.ai.tuling123.com/speech/chat";
HttpPost httpPost = new HttpPost(url);
System.out.println("post url:"+url);
//请求头的设置
httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
httpPost.setHeader("Accept-Language","en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2,es;q=0.2");
httpPost.setHeader("Accept","*/*");
httpPost.setHeader("Accept-Encoding","gzip, deflate");
httpPost.setHeader("Connection","keep-alive");
httpPost.setHeader("Cache-Control","no-cache");
MultipartEntity mutiEntity = new MultipartEntity();
//测试的语音文件,可以上网下载素材wav文件
File file = new File("d:/1.wav");
//它分为两部分,一部分是parameters文本参数,一部分是speech 语音参数
mutiEntity.addPart("parameters",new StringBody( JSONObject.toJSONString(map), Charset.forName("utf-8")));
mutiEntity.addPart("speech", new FileBody(file));
DefaultHttpClient httpClient = new DefaultHttpClient();
httpPost.setEntity(mutiEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String content = EntityUtils.toString(httpEntity);
System.out.println(content);
}
3、测试结果:
post url:http://smartdevice.ai.tuling123.com/speech/chat
{"code":20000,"asr":"嗯。","tts":"听到你答应了,人家好开心呀!","nlp":["http://turing-iot.oss-cn-beijing.aliyuncs.com/audio/nlp-184ce73de69b42a49d868527cf707143-38e1c4780ca342808ec085a75b2fd9a5.mp3"],"token":"4552a209fee3422aa6e998579a0f3311"}