Ich versuche, den Text zum Sprachergebnis von meiner Android-App an meinen PC über TCP-Socket zu senden. Ich implementiere es mit Async wie erforderlich.Abrufen der NetworkOnMainThread-Ausnahme auch nach der Verwendung von AsyncTask
Hier ist die onCreate()
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sender = new TCPClient("192.168.0.7",2016);
}
auf den Text-zu-Sprache Ergebnis Empfang Ich rufe die execute()
Methode.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
sender.execute(result.get(0));
}
break;
}
}
}
Das ist mein TCPClient Klasse:
public class TCPClient extends AsyncTask<String,String,String>
{
protected void onPostExecute(String result) {
}
protected void onPreExecute1() {}
protected void onProgressUpdate(Void... values) {}
PrintWriter out;
TCPClient(String ip, int port)
{
try
{
Socket socket = new Socket(ip,port);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
}catch (Exception e)
{
Log.d("LINUX","Exception while creating socket");
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... arg0)
{
Log.d("LINUX","Data sent : "+arg0);
out.println(arg0);
return "Executed";
}
}
Aber noch bin ich NetworkOnMainThreadException
bekommen. Kann mir bitte jemand helfen.
Hier ist der stacktrace:
06-23 01:42:03.651: W/System.err(11990): android.os.NetworkOnMainThreadException
06-23 01:42:03.652: W/System.err(11990): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1161)
06-23 01:42:03.653: W/System.err(11990): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
06-23 01:42:03.653: W/System.err(11990): at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
06-23 01:42:03.653: W/System.err(11990): at libcore.io.IoBridge.connect(IoBridge.java:122)
06-23 01:42:03.653: W/System.err(11990): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
06-23 01:42:03.653: W/System.err(11990): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
06-23 01:42:03.653: W/System.err(11990): at java.net.Socket.startupSocket(Socket.java:590)
06-23 01:42:03.653: W/System.err(11990): at java.net.Socket.tryAllAddresses(Socket.java:128)
06-23 01:42:03.653: W/System.err(11990): at java.net.Socket.<init>(Socket.java:178)
06-23 01:42:03.653: W/System.err(11990): at java.net.Socket.<init>(Socket.java:150)
06-23 01:42:03.654: W/System.err(11990): at info.androidhive.speechtotext.TCPClient.<init>(TCPClient.java:26)
06-23 01:42:03.654: W/System.err(11990): at info.androidhive.speechtotext.MainActivity.onCreate(MainActivity.java:28)
06-23 01:42:03.654: W/System.err(11990): at android.app.Activity.performCreate(Activity.java:6010)
06-23 01:42:03.654: W/System.err(11990): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
06-23 01:42:03.654: W/System.err(11990): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
06-23 01:42:03.654: W/System.err(11990): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
06-23 01:42:03.654: W/System.err(11990): at android.app.ActivityThread.access$800(ActivityThread.java:155)
06-23 01:42:03.654: W/System.err(11990): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
06-23 01:42:03.654: W/System.err(11990): at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 01:42:03.654: W/System.err(11990): at android.os.Looper.loop(Looper.java:135)
06-23 01:42:03.654: W/System.err(11990): at android.app.ActivityThread.main(ActivityThread.java:5343)
06-23 01:42:03.654: W/System.err(11990): at java.lang.reflect.Method.invoke(Native Method)
06-23 01:42:03.654: W/System.err(11990): at java.lang.reflect.Method.invoke(Method.java:372)
06-23 01:42:03.654: W/System.err(11990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
06-23 01:42:03.654: W/System.err(11990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Nur DoInBackground ist im Hintergrund Thread, also was ist zu erwarten? – Selvin