diff --git a/app/src/main/java/info/androidhive/speechtotext/MainActivity.java b/app/src/main/java/info/androidhive/speechtotext/MainActivity.java index 5dca955..ed26b41 100644 --- a/app/src/main/java/info/androidhive/speechtotext/MainActivity.java +++ b/app/src/main/java/info/androidhive/speechtotext/MainActivity.java @@ -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{ + + @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); + } + }