我创建了一个程序来读取语音,将其传输到文本,然后通过WiFi发送到服务器。
语音识别工作正常。但是服务器上没有接收到数据。但是,当与语音识别分开使用时,客户端服务器工作正常。
继承我的代码..
public class MainActivity extends Activity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView txtText;
public ArrayList<String> text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtText = (TextView) findViewById(R.id.txtText);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//txtText.setText(text.get(0));
//
//Log.d("inputString", text.get(0));
new Thread(new Runnable() {
public void run() {
try{
txtText.setText("excepion in");
URL url = new URL("http://192.168.137.1:8080/SpeeCom/DoubleMeServlet");
//URL url = new URL("http://10.0.2.2:8080/server/DoubleMeServlet");
URLConnection connection = url.openConnection();
//String inputString = inputValue.getText().toString();
//inputString = URLEncoder.encode(inputString, "UTF-8");
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(text.get(0));
out.close();
}catch(Exception e)
{
txtText.setText("excepion" + e);
}
}
}).start();
//
}
break;
}
}
}
}
答案 0 :(得分:0)
尝试通过检查值发送来接近问题:
try{
txtText.setText("excepion in");
URL url = new URL("http://192.168.137.1:8080/SpeeCom/DoubleMeServlet");
//URL url = new URL("http://10.0.2.2:8080/server/DoubleMeServlet");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
String mess = "start--" + text.get(0);
Log.d(mess);
out.write(mess);
}catch(Exception e){
txtText.setText("excepion" + e);
e.printStackTrace();
}finally{
out.close();
}
输出是什么?