I've created an app that communicates with Socket communication Press button to send content in editText. If the socket is connected well, there will be no error If you are not connected, press the button to force the app to shut down.
This is the main activity.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ConnectException; import java.net.Socket;
import android.app.Activity; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;
public class socket extensions Activity { //main activity
Socket socket; // Create socket
Buffered Reader in; //Read data from the server.
PrintWriter out; //Send data to the server.
EditText input; //xml element
Button button; //xml element
TextView output; //xml element
String data; //Socket data storage
@Override
protected void on Create(Bundle saved InstanceState) { //Setting initialization at app startup
super.onCreate(savedInstanceState);
setContentView (R.layout.socket_lay); //Load Layout
input = (EditText) findViewById(R.id.input); // character input (edit text)
button = (Button) findViewById(R.id.Button); // Send button
output = (TextView) findViewById(R.id.output); // Received data
Button.setOnClickListener() { //The part you send to button
When the public void on Click(View) { // button is clicked, it outputs data to the socket.
String data = input.getText().toString(); //Receive the characters in the character input box in String form and store them in the data
Log.w("NETWORK", " " + data);
If (data!=null) { //if no data is entered
out.println(data); //data is transformed into a stream form and transmitted. The transformation is contained in the thread.
}
}
});
Create Threadworker = new Thread() { //worker as Thread
public void run() { // thread execution syntax
Create a try { // socket and connect the input/output strip to the socket.
socket = new socket ("192.168.4.1", 8080); //Socket connection
out = new PrintWriter(socket.getOutputStream(),true); //Transfer data to stream form when transmitting.
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));//Accept stream when receiving data.
} catch (IOException) { //Additional Description https://code.i-harness.com/ko-kr/q/c9a994
e.printStackTrace();
Toast.makeText (com.example.jbts201604.socket_37.socket.this,"ConnectionError", Toast.LENGTH_SHORT).show();
}
try {
while (true) {
Data = in.readLine(); // read data received in String format and stored in data
output.post(new Runnable() {
public void run() {
Output.setText(data); //print messages sent by the server in the character output box.
}
});
}
} } catch (Exception e) {
Toast.makeText (com.example.jbts201604.socket_37.socket.this",Toast.LENGTH_SHORT).show();
}
}
};
Run at worker.start(); //onResume().
}
@Override
protected void onStop() { //When the app is closed
super.onStop();
try {
socket.close(); // Close the socket.
} } catch (IOException e) {
e.printStackTrace();
}
}
}
This is the content of the logcat.
09-05 12:17:40.899 3195-3195/? D/dalvikvm: Not late-enabling CheckJNI (already on)
09-05 12:17:44.769 3195-3195/com.example.jbts201604.socket_37 D/EGL_emulation: eglCreateContext: 0xb7d92fd0: maj 2 min 0 rcv 2
09-05 12:17:44.829 3195-3195/com.example.jbts201604.socket_37 D/EGL_emulation: eglMakeCurrent: 0xb7d92fd0: ver 2 0
09-05 12:17:44.829 3195-3195/com.example.jbts201604.socket_37 E/EGL_emulation: tid 3195: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH)
09-05 12:17:44.829 3195-3195/com.example.jbts201604.socket_37 W/HardwareRenderer: Backbuffer cannot be preserved
09-05 12:17:44.829 3195-3195/com.example.jbts201604.socket_37 D/OpenGLRenderer: Enabling debug mode 0
09-05 12:17:48.479 3195-3195/com.example.jbts201604.socket_37 W/NETWORK:
09-05 12:17:48.479 3195-3195/com.example.jbts201604.socket_37 D/AndroidRuntime: Shutting down VM
09-05 12:17:48.479 3195-3195/com.example.jbts201604.socket_37 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x9cccfb20)
09-05 12:17:48.479 3195-3195/com.example.jbts201604.socket_37 D/dalvikvm: GC_FOR_ALLOC freed 70K, 4% free 3436K/3576K, paused 2ms, total 2ms
09-05 12:17:48.479 3195-3195/com.example.jbts201604.socket_37 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jbts201604.socket_37, PID: 3195
java.lang.NullPointerException
at com.example.jbts201604.socket_37.socket$1.onClick(socket.java:48)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Due to the strengthening of Android security rules, tasks such as network, fileio, db, etc. will crash when they operate on UI Thread. Network-related tasks (socket connection binding) Try to handle them using threads or handlers.
According to the log, it seems that a crash occurs while working on the socket on the main thread.
If you look at the log, an exception is occurring in onClick().
String data = input.getText().toString();
where data
is not always null
The likely part of the out
variable for the nullPointerException to occur. Therefore, PrintWriter
object cannot be created when socket connection is not made, and NullPointerException
by out variable appears to occur.
© 2024 OneMinuteCode. All rights reserved.