Sfoglia il codice sorgente

Add interface to Callback

Alec 4 anni fa
parent
commit
d7e4da9a8d

+ 5 - 0
app/src/main/java/com/example/chatclient/Callback.java

@@ -0,0 +1,5 @@
+package com.example.chatclient;
+
+public interface Callback {
+    void RecvMessage(String msg);
+}

+ 19 - 9
app/src/main/java/com/example/chatclient/Client.java

@@ -14,24 +14,29 @@ import java.net.Socket;
 public class Client extends Thread {
 
     WriteThread writeThread;
+    Callback callback;
 
     public void send(String msg) {
         writeThread.setMessage(msg);
     }
 
+    public Client(Callback callback) {
+        this.callback = callback;
+    }
+
     @Override
     public void run() {
         Socket socket = null;
         try {
             socket = new Socket("192.168.0.2", 9125);
 
-//            ReadThread readThread = new ReadThread(socket);
+            ReadThread readThread = new ReadThread(socket, callback);
             writeThread = new WriteThread(socket);
 
-//            readThread.start();
+            readThread.start();
             writeThread.start();
 
-//            readThread.join();
+            readThread.join();
             writeThread.join();
 
         } catch (IOException | InterruptedException e) {
@@ -50,23 +55,28 @@ public class Client extends Thread {
 class ReadThread extends Thread {
     private Socket socket;
 
-    public ReadThread(Socket socket) {
+    private Callback callback;
+
+    public ReadThread(Socket socket, Callback callback) {
         this.socket = socket;
+        this.callback = callback;
     }
 
     @Override
     public void run() {
         while (!isInterrupted()) {
             try {
-                DataInputStream ois = new DataInputStream(socket.getInputStream());
+                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
 
-                String o = ois.readLine();
+                Transport t = (Transport) ois.readObject();
 
-                System.out.print("\033[1;31m\033[40m " + o + ": \u001B[0m ");
-                System.out.println(o);
+                System.out.print("\033[1;31m\033[40m " + t.Name + ": \u001B[0m ");
+                System.out.println(t.Message);
 
+//                textView.setText(t.Name + ": " + t.Message);
+                callback.RecvMessage(t.Message);
 
-            } catch (IOException exception) {
+            } catch (IOException | ClassNotFoundException exception) {
                 exception.printStackTrace();
                 try {
                     socket.close();

+ 20 - 2
app/src/main/java/com/example/chatclient/MainActivity.java

@@ -10,12 +10,26 @@ import android.widget.TextView;
 
 import java.net.Socket;
 
-public class MainActivity extends AppCompatActivity {
+public class MainActivity extends AppCompatActivity implements Callback {
 
+    TextView textView2;
     EditText textView;
     Button button;
     Client client;
 
+    @Override
+    public void RecvMessage(String msg) {
+
+        final String m = msg;
+
+        runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                textView2.setText(m);
+            }
+        });
+    }
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -24,7 +38,9 @@ public class MainActivity extends AppCompatActivity {
         textView = findViewById(R.id.textView);
         button = findViewById(R.id.sendBtn);
 
-        client = new Client();
+        textView2 = findViewById(R.id.textView2);
+
+        client = new Client(this);
         client.start();
 
         button.setOnClickListener(new View.OnClickListener() {
@@ -34,4 +50,6 @@ public class MainActivity extends AppCompatActivity {
             }
         });
     }
+
+
 }

+ 13 - 11
app/src/main/res/layout/activity_main.xml

@@ -6,23 +6,15 @@
     android:layout_height="match_parent"
     tools:context=".MainActivity">
 
-    <TextView
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="Hello World!"
-        app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintLeft_toLeftOf="parent"
-        app:layout_constraintRight_toRightOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
-
     <Button
         android:id="@+id/sendBtn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:layout_marginStart="28dp"
+        android:layout_marginStart="88dp"
+        android:layout_marginTop="156dp"
         android:text="Button"
         app:layout_constraintStart_toStartOf="parent"
-        tools:layout_editor_absoluteY="112dp" />
+        app:layout_constraintTop_toBottomOf="@+id/textView" />
 
     <EditText
         android:id="@+id/textView"
@@ -34,4 +26,14 @@
         tools:layout_editor_absoluteX="28dp"
         tools:layout_editor_absoluteY="31dp" />
 
+    <TextView
+        android:id="@+id/textView2"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="96dp"
+        android:layout_marginTop="76dp"
+        android:text="TextView"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/sendBtn" />
+
 </androidx.constraintlayout.widget.ConstraintLayout>