Communicate with server
This commit is contained in:
parent
aada3c4b3b
commit
86469b0d3d
@ -1,13 +1,16 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="info.androidhive.speechtotext"
|
package="info.androidhive.speechtotext"
|
||||||
android:versionCode="1"
|
android:versionCode="1"
|
||||||
android:versionName="1.0" >
|
android:versionName="1.0" >
|
||||||
|
|
||||||
<uses-sdk
|
<uses-sdk
|
||||||
android:minSdkVersion="11"
|
android:minSdkVersion="19"
|
||||||
android:targetSdkVersion="19" />
|
android:targetSdkVersion="19" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@drawable/ic_launcher"
|
android:icon="@drawable/ic_launcher"
|
||||||
|
@ -1,13 +1,27 @@
|
|||||||
package info.androidhive.speechtotext;
|
package info.androidhive.speechtotext;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.NetworkInfo;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.speech.RecognizerIntent;
|
import android.speech.RecognizerIntent;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
@ -19,6 +33,9 @@ public class MainActivity extends Activity {
|
|||||||
private TextView txtSpeechInput;
|
private TextView txtSpeechInput;
|
||||||
private ImageButton btnSpeak;
|
private ImageButton btnSpeak;
|
||||||
private final int REQ_CODE_SPEECH_INPUT = 100;
|
private final int REQ_CODE_SPEECH_INPUT = 100;
|
||||||
|
public static final String TAG = MainActivity.class.getSimpleName();
|
||||||
|
public static final String Url = "http://192.168.10.109:1234";
|
||||||
|
public static final int MAX_ANSWER_LEN = 500;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -63,7 +80,7 @@ public class MainActivity extends Activity {
|
|||||||
/**
|
/**
|
||||||
* Receiving speech input
|
* Receiving speech input
|
||||||
* */
|
* */
|
||||||
@Override<>
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
|
||||||
@ -74,6 +91,15 @@ public class MainActivity extends Activity {
|
|||||||
ArrayList<String> result = data
|
ArrayList<String> result = data
|
||||||
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
|
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
|
||||||
txtSpeechInput.setText(result.get(0));
|
txtSpeechInput.setText(result.get(0));
|
||||||
|
|
||||||
|
ConnectivityManager connMgr = (ConnectivityManager)
|
||||||
|
getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
|
||||||
|
if (networkInfo != null && networkInfo.isConnected()) {
|
||||||
|
new TalkToMeTask().execute(result.get(0));
|
||||||
|
} else {
|
||||||
|
txtSpeechInput.setText("No network connection available.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -88,4 +114,65 @@ public class MainActivity extends Activity {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class TalkToMeTask extends AsyncTask<String,Void, String>{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(String... question) {
|
||||||
|
try {
|
||||||
|
return talkToServer(question[0]);
|
||||||
|
} catch (IOException e){
|
||||||
|
Log.d(TAG, "Coincoin "+e);
|
||||||
|
return "Cannot connect to server";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(String s) {
|
||||||
|
txtSpeechInput.setText(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user