質問編集履歴
3
ソースコードを変更、コメントを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,30 +6,59 @@
|
|
6
6
|
必須パラメータがない とエラーが帰ってくる
|
7
7
|
POSTはできている模様です
|
8
8
|
```
|
9
|
+
/*
|
10
|
+
* To change this license header, choose License Headers in Project Properties.
|
11
|
+
* To change this template file, choose Tools | Templates
|
12
|
+
* and open the template in the editor.
|
13
|
+
*/
|
14
|
+
package libs;
|
15
|
+
|
16
|
+
import java.io.BufferedReader;
|
17
|
+
import java.io.BufferedWriter;
|
18
|
+
import java.io.IOException;
|
19
|
+
import java.io.InputStream;
|
20
|
+
import java.io.InputStreamReader;
|
21
|
+
import java.io.OutputStreamWriter;
|
22
|
+
import java.net.MalformedURLException;
|
23
|
+
import java.net.*;
|
24
|
+
import java.nio.charset.Charset;
|
25
|
+
import java.nio.charset.StandardCharsets;
|
26
|
+
import java.nio.file.FileSystem;
|
27
|
+
import java.nio.file.FileSystems;
|
28
|
+
import java.nio.file.Files;
|
29
|
+
import java.nio.file.Path;
|
30
|
+
import java.util.logging.Level;
|
31
|
+
import java.util.logging.Logger;
|
32
|
+
/**
|
33
|
+
*
|
34
|
+
* @author KITT
|
35
|
+
*/
|
36
|
+
public class test {
|
37
|
+
|
38
|
+
public test() throws IOException{
|
9
|
-
String boundary ="JT49KPkVKrz2Y4YrGhLezUP8fWHU6kARBrpKRpuNsjXm";
|
39
|
+
String boundary ="JT49KPkVKrz2Y4YrGhLezUP8fWHU6kARBrpKRpuNsjXm";
|
40
|
+
String based=null;//base64に変換後、文字列として格納
|
10
|
-
|
41
|
+
FileSystem fs = FileSystems.getDefault();
|
42
|
+
Path p = fs.getPath("C:\voice.raw");
|
43
|
+
Charset charset = StandardCharsets.UTF_8;//キャラセットをUTF-8にする
|
44
|
+
byte[] vb = Files.readAllBytes(p);//voice.rawを読み込む
|
11
45
|
try {
|
12
|
-
URL url =new URL("https://api.apigw.smt.docomo.ne.jp/amiVoice/v1/recognize?APIKEY=");/
|
46
|
+
URL url =new URL("https://api.apigw.smt.docomo.ne.jp/amiVoice/v1/recognize?APIKEY=XXXXXXXXXXXXXXXXX");//APIkeyは省略
|
13
47
|
URLConnection con=url.openConnection();
|
14
48
|
con.setDoOutput(true);
|
15
|
-
|
49
|
+
|
16
50
|
OutputStreamWriter writer=new OutputStreamWriter(con.getOutputStream());
|
17
51
|
BufferedWriter bw1 = new BufferedWriter(writer);
|
18
52
|
/*送信文字列*/
|
19
|
-
bw1.write("Content-Type: multipart/form-data; boundary=<"+boundary+">\r\nContent-Disposition: form-data; name=\"v\"\r\n\r\n");
|
53
|
+
bw1.write("Content-Type: multipart/form-data; boundary=<"+boundary+">\r\n--<"+boundary+">\r\nContent-Disposition: form-data; name=\"v\"\r\n\r\n");
|
20
|
-
bw1.write("on\r\n--<
|
54
|
+
bw1.write("on\r\n--<"+boundary+">\r\n");
|
21
55
|
bw1.write("Content-Disposition: form-data; name=\"a\";filename=\"voice.raw\"\r\n");
|
22
56
|
bw1.write("Content-Type: application/octet-stream");
|
57
|
+
bw1.write("\r\n\r\n");
|
58
|
+
java.util.Base64.Encoder Encoder = java.util.Base64.getEncoder();
|
59
|
+
based=Encoder.encodeToString(vb);
|
60
|
+
bw1.write(based);
|
23
61
|
bw1.write("\r\n");
|
24
|
-
FileInputStream fi=new FileInputStream(voice);
|
25
|
-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
26
|
-
while (fi.read(vb) > 0) {
|
27
|
-
baos.write(vb);
|
28
|
-
}
|
29
|
-
String vba=Encoder.encodeToString(vb);
|
30
|
-
|
31
|
-
bw1.write(vba);
|
32
|
-
bw1.write("\r\n");
|
33
62
|
bw1.write("--<"+boundary+">--");
|
34
63
|
bw1.flush();
|
35
64
|
bw1.close();
|
@@ -37,15 +66,17 @@
|
|
37
66
|
InputStream stream = con.getInputStream();
|
38
67
|
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
39
68
|
String responseData = null;
|
40
|
-
|
69
|
+
while((responseData = reader.readLine()) != null){
|
41
|
-
|
70
|
+
System.out.print(responseData);
|
42
|
-
|
71
|
+
}
|
43
|
-
|
72
|
+
stream.close();
|
44
73
|
} catch (MalformedURLException ex) {
|
45
74
|
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
|
46
75
|
}
|
47
|
-
|
76
|
+
}}
|
48
77
|
|
78
|
+
|
79
|
+
|
49
80
|
###該当のソースコード```Java
|
50
81
|
コード
|
51
82
|
```
|
@@ -53,5 +84,7 @@
|
|
53
84
|
###試したこと
|
54
85
|
APIリファレンス通りに文字列を入力した
|
55
86
|
文字列のfilenameをvoice.rawにした
|
87
|
+
キャラセットをUTF-8にしたり、outputstreamのキャラセットをUTF-8にしたりしたが、同じ返答結果になる
|
88
|
+
|
56
89
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
57
|
-
netbeans jdk1.8
|
90
|
+
netbeans jdk1.8 win10
|
2
試したことを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -52,5 +52,6 @@
|
|
52
52
|
PYTHONでのPOSTには成功していますが、JAVAでPOSTしたいと思い作成中です
|
53
53
|
###試したこと
|
54
54
|
APIリファレンス通りに文字列を入力した
|
55
|
+
文字列のfilenameをvoice.rawにした
|
55
56
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
56
57
|
netbeans jdk1.8
|
1
APIリファレンスのURLを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
###前提・実現したいこと
|
2
2
|
javaでdocomoAPI音声認識にPOSTしたいのですがエラーがでる
|
3
|
-
|
3
|
+
https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=speech_recognition&p_name=api_amivoice_1#tag01
|
4
|
+
APIリファレンスです
|
4
5
|
###発生している問題・エラーメッセージ
|
5
6
|
必須パラメータがない とエラーが帰ってくる
|
6
7
|
POSTはできている模様です
|