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
1 changed files with 55 additions and 37 deletions

View File

@ -55,6 +55,7 @@ public class MainActivity extends Activity {
promptSpeechInput(); promptSpeechInput();
} }
}); });
new SilentTalkToMeTask().execute("goodbye");
} }
@ -130,49 +131,66 @@ public class MainActivity extends Activity {
protected void onPostExecute(String s) { protected void onPostExecute(String s) {
txtSpeechInput.setText(s); txtSpeechInput.setText(s);
} }
}
private String talkToServer(String question) throws IOException { private class SilentTalkToMeTask extends AsyncTask<String,Void, String>{
InputStream is = null;
@Override
protected String doInBackground(String... question) {
try { try {
URL url = new URL(Url); return talkToServer(question[0]);
String urlParameters = "msg="+question; } catch (IOException e){
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 ); Log.d(TAG, "Coincoin "+e);
int postDataLength = postData.length; return "Cannot connect to server";
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 { @Override
Reader reader = null; protected void onPostExecute(String s) {
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
} }
} }
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);
}
} }