Android TCP-Verbindung Byte senden? -> NetworkOnMainThreadException

  • Java

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von Gelöschter Benutzer.

    Android TCP-Verbindung Byte senden? -> NetworkOnMainThreadException

    Hallo,

    Ich hatte heute mal die Idee, mir einen Media-Remote-Control für mein Handy zu programmieren.
    Da ich (relativ) neu in Android-Programmierung bin, würde ich euch gerne um Hilfe bitten :) :

    Bis jetzt habe ich:
    - (funktionierenden) Server auf meinem Computer (C#)
    - Grundgerüst (Controls, etc.) für meine App (Java^^)
    Mein Problem ist jetzt, dass ich nicht weiß, wie ich eine TCP-Verbindung zwischen Android und PC herstelle, da im HauptThread das anscheinend nicht gemacht werden kann (android.os.NetworkOnMainThreadException).

    PS: Es sollen nur Titel/Interpret empfangen werden und 0-6 (bytes) für Play/Pause/Vol+/Vol-/etc. gesendet werden. (!)Das ganze soll/kann nur über Internes Netzwerk laufen, muss aber nicht.

    App:
    MainActivity.java

    Java-Quellcode

    1. package com.flammalpha.mediaclient;
    2. import java.io.*;
    3. import java.net.*;
    4. import android.os.Bundle;
    5. import android.app.Activity;
    6. import android.view.*;
    7. import android.widget.*;
    8. public class MainActivity extends Activity {
    9. Socket client;
    10. PrintWriter out;
    11. BufferedReader in;
    12. ServerSocket server;
    13. public void sendPrev(View view)
    14. {
    15. Send(2);
    16. }
    17. public void sendPlayPause(View view)
    18. {
    19. Send(0);
    20. }
    21. public void sendStop(View view)
    22. {
    23. Send(3);
    24. }
    25. public void sendNext(View view)
    26. {
    27. Send(1);
    28. }
    29. public void sendVolDown(View view)
    30. {
    31. Send(5);
    32. }
    33. public void sendMute(View view)
    34. {
    35. Send(6);
    36. }
    37. public void sendVolUp(View view)
    38. {
    39. Send(4);
    40. }
    41. public void Send(int val)
    42. {
    43. try
    44. {
    45. out.println(val);
    46. out.flush();
    47. }
    48. catch (Exception ex)
    49. {
    50. ((TextView)findViewById(R.id.tvStatus)).setText("Unable to send");
    51. }
    52. }
    53. public void clickConnect(View view)
    54. {
    55. execute(Connect());
    56. }
    57. public void Connect(){
    58. try
    59. {
    60. client = new Socket("192.168.178.16", 4664);
    61. out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
    62. in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    63. ((TextView)findViewById(R.id.tvStatus)).setText("Connected to: 192.168.178.16");
    64. }
    65. catch (UnknownHostException e)
    66. {
    67. ((TextView)findViewById(R.id.tvStatus)).setText("Unknown host: 192.168.178.16");
    68. }
    69. catch (IOException e)
    70. {
    71. ((TextView)findViewById(R.id.tvStatus)).setText("No I/O");
    72. }
    73. }
    74. @Override
    75. protected void onCreate(Bundle savedInstanceState) {
    76. super.onCreate(savedInstanceState);
    77. setContentView(R.layout.activity_main);
    78. }
    79. }
    activity-main.xml

    XML-Quellcode

    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:paddingBottom="@dimen/activity_vertical_margin"
    6. android:paddingLeft="@dimen/activity_horizontal_margin"
    7. android:paddingRight="@dimen/activity_horizontal_margin"
    8. android:paddingTop="@dimen/activity_vertical_margin"
    9. android:scrollbars="none"
    10. tools:context=".MainActivity" >
    11. <TextView
    12. android:id="@+id/tvInterpret"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:layout_alignParentLeft="true"
    16. android:layout_alignParentRight="true"
    17. android:layout_alignParentTop="true"
    18. android:scrollbarAlwaysDrawVerticalTrack="false"
    19. android:scrollbars="none"
    20. android:text="Interpret"
    21. android:textAppearance="?android:attr/textAppearanceLarge" />
    22. <TextView
    23. android:id="@+id/tvTitle"
    24. android:layout_width="wrap_content"
    25. android:layout_height="wrap_content"
    26. android:layout_alignParentLeft="true"
    27. android:layout_alignParentRight="true"
    28. android:layout_below="@+id/tvInterpret"
    29. android:text="Title"
    30. android:textAppearance="?android:attr/textAppearanceLarge" />
    31. <LinearLayout
    32. android:id="@+id/linearLayout1"
    33. android:layout_width="fill_parent"
    34. android:layout_height="wrap_content"
    35. android:layout_alignParentBottom="true"
    36. android:layout_alignParentLeft="true"
    37. android:layout_alignParentRight="true"
    38. android:orientation="vertical" >
    39. <LinearLayout
    40. android:id="@+id/PlayLayout"
    41. android:layout_width="fill_parent"
    42. android:layout_height="wrap_content" >
    43. <Button
    44. android:id="@+id/btnPrev"
    45. android:layout_width="wrap_content"
    46. android:layout_height="wrap_content"
    47. android:onClick="sendPrev"
    48. android:text="&lt;&lt;" />
    49. <Button
    50. android:id="@+id/btnPlayPause"
    51. android:layout_width="wrap_content"
    52. android:layout_height="wrap_content"
    53. android:onClick="sendPlayPause"
    54. android:text="|| ►" />
    55. <Button
    56. android:id="@+id/btnStop"
    57. android:layout_width="wrap_content"
    58. android:layout_height="wrap_content"
    59. android:onClick="sendStop"
    60. android:text="■" />
    61. <Button
    62. android:id="@+id/btnNext"
    63. android:layout_width="wrap_content"
    64. android:layout_height="wrap_content"
    65. android:onClick="sendNext"
    66. android:text=">>" />
    67. </LinearLayout>
    68. <LinearLayout
    69. android:id="@+id/VolumeLayout"
    70. android:layout_width="fill_parent"
    71. android:layout_height="wrap_content" >
    72. <Button
    73. android:id="@+id/btnVolDown"
    74. android:layout_width="wrap_content"
    75. android:layout_height="wrap_content"
    76. android:onClick="sendVolDown"
    77. android:text="-" />
    78. <Button
    79. android:id="@+id/btnMute"
    80. android:layout_width="wrap_content"
    81. android:layout_height="wrap_content"
    82. android:onClick="sendMute"
    83. android:text="Mute" />
    84. <Button
    85. android:id="@+id/btnVolUp"
    86. android:layout_width="wrap_content"
    87. android:layout_height="wrap_content"
    88. android:onClick="sendVolUp"
    89. android:text="+" />
    90. <Button
    91. android:id="@+id/btnConnect"
    92. android:layout_width="wrap_content"
    93. android:layout_height="wrap_content"
    94. android:onClick="clickConnect"
    95. android:text="->" />
    96. </LinearLayout>
    97. </LinearLayout>
    98. <TextView
    99. android:id="@+id/tvStatus"
    100. android:layout_width="wrap_content"
    101. android:layout_height="wrap_content"
    102. android:layout_above="@+id/linearLayout1"
    103. android:layout_alignParentLeft="true"
    104. android:layout_alignParentRight="true"
    105. android:text="Status"
    106. android:textAppearance="?android:attr/textAppearanceSmall" />
    107. </RelativeLayout>

    Vielen Dank im Voraus!,
    Valerian
    Hey,

    Danke für die schnelle Antwort :) . Das war jetzt eine ganze Chat-Anwendung, da hab ich schon fast wieder den Überblick verloren :D . Ich werde aber mal versuchen das Wichtige rauszufischen. Bin aber immer für weitere Hilfe offen :) !

    PS: das "execute(Connect());" ist eine non-sense Zeile.
    Valerian

    //EDIT: danke hab alles gefunden :) ist super cool aus dem Wohnzimmer die Musik im Arbeitszimmer zu steuern! :D

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Gelöschter Benutzer“ ()