Start the application by finishing previous talk

This commit is contained in:
Mathieu Maret 2016-01-20 17:47:11 +01:00
parent 86469b0d3d
commit 1dc5fc78f9

View File

@ -55,6 +55,7 @@ public class MainActivity extends Activity {
promptSpeechInput();
}
});
new SilentTalkToMeTask().execute("goodbye");
}
@ -130,49 +131,66 @@ public class MainActivity extends Activity {
protected void onPostExecute(String s) {
txtSpeechInput.setText(s);
}
}
private String talkToServer(String question) throws IOException {
InputStream is = null;
private class SilentTalkToMeTask extends AsyncTask<String,Void, String>{
@Override
protected String doInBackground(String... question) {
try {
URL url = new URL(Url);
String urlParameters = "msg="+question;
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
conn.setUseCaches(false);
DataOutputStream wr = new DataOutputStream( conn.getOutputStream());
wr.write(postData);
conn.connect();
int response = conn.getResponseCode();
Log.d(TAG, "The response is: " + response);
is = conn.getInputStream();
String contentAsString = readIt(is, MAX_ANSWER_LEN);
return contentAsString;
} finally {
if (is != null){
is.close();
}
return talkToServer(question[0]);
} catch (IOException e){
Log.d(TAG, "Coincoin "+e);
return "Cannot connect to server";
}
}
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
@Override
protected void onPostExecute(String s) {
}
}
private String talkToServer(String question) throws IOException {
InputStream is = null;
try {
URL url = new URL(Url);
String urlParameters = "msg="+question;
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
conn.setUseCaches(false);
DataOutputStream wr = new DataOutputStream( conn.getOutputStream());
wr.write(postData);
conn.connect();
int response = conn.getResponseCode();
Log.d(TAG, "The response is: " + response);
is = conn.getInputStream();
String contentAsString = readIt(is, MAX_ANSWER_LEN);
return contentAsString;
} finally {
if (is != null){
is.close();
}
}
}
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
}