I am trying to develop a project in which I want to get a String input, pass it as a parameter in a function in the app in my MainActivity file.
I have written the TCP-server initialization in Java and the plan is to start the server on my Desktop and then just get in the command the above mentioned String input from my Android app locally.
The problem though is that even though the server seems to start and so does the app on my emulator whenever some there is input to be sent the app crashes.
The server code:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args){
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
dataOutputStream.writeUTF("Hello!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if( socket!= null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataInputStream!= null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataOutputStream!= null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
And here is the function from Android.
public void sendData(String text) {
// TODO Auto-generated method stub
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("/*IPv4 Address*/", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
//dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(text);
//textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
I think the problem exists in the function and not the rest of the android code because without it everything runs normally
This is the exception I get
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.github.chagall.notificationlistenerexample, PID: 8957
> java.lang.RuntimeException: Error receiving broadcast Intent { act=com.github.chagall.notificationlistenerexample flg=0x10 (has
> extras) } in
> com.github.chagall.notificationlistenerexample.MainActivity$ImageChangeBroadcastReceiver@ef6eb1d
> at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52166(LoadedApk.java:1327)
> at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.$m$0(Unknown
> Source:4)
> at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.run(Unknown Source:0)
> at android.os.Handler.handleCallback(Handler.java:789)
> at android.os.Handler.dispatchMessage(Handler.java:98)
> at android.os.Looper.loop(Looper.java:164)
> at android.app.ActivityThread.main(ActivityThread.java:6938)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
> Caused by: android.os.NetworkOnMainThreadException
> at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
> at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:355)
> at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
> at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
> at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
> at java.net.Socket.connect(Socket.java:616)
> at java.net.Socket.connect(Socket.java:565)
> at java.net.Socket.<init>(Socket.java:445)
> at java.net.Socket.<init>(Socket.java:217)
> at com.github.chagall.notificationlistenerexample.MainActivity.sendData(MainActivity.java:217)
> at com.github.chagall.notificationlistenerexample.MainActivity.changeInterceptedNotificationImage(MainActivity.java:118)
> at com.github.chagall.notificationlistenerexample.MainActivity.access$000(MainActivity.java:48)
> at com.github.chagall.notificationlistenerexample.MainActivity$ImageChangeBroadcastReceiver.onReceive(MainActivity.java:178)
> at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52166(LoadedApk.java:1317)
> at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.$m$0(Unknown
> Source:4)
> at android.app.-$Lambda$FilBqgnXJrN9Mgyks1XHeAxzSTk.run(Unknown
> Source:0)
> at android.os.Handler.handleCallback(Handler.java:789)
> at android.os.Handler.dispatchMessage(Handler.java:98)
> at android.os.Looper.loop(Looper.java:164)
> at android.app.ActivityThread.main(ActivityThread.java:6938)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Any suggestions appreciated.