Fatal exception in android

I wrote simple code for client server sockets as follows

MyServer.java

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class MyServer { 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(); } } } } }} 

The android client is as follows

MyClientActivity.java

 package com.exercise.AndroidClient; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.os.Bundle; public class MyClientActivity extends Activity { EditText textOut; TextView textIn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textOut = (EditText)findViewById(R.id.textout); Button buttonSend = (Button)findViewById(R.id.send); textIn = (TextView)findViewById(R.id.textin); buttonSend.setOnClickListener(buttonSendOnClickListener); } Button.OnClickListener buttonSendOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Socket socket = null; DataOutputStream dataOutputStream = null; DataInputStream dataInputStream = null; try { socket = new Socket("113.193.42.220", 8888); dataOutputStream = new DataOutputStream(socket.getOutputStream()); dataInputStream = new DataInputStream(socket.getInputStream()); dataOutputStream.writeUTF(textOut.getText().toString()); 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(); } } } }}; } 

main.xml

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/textout" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Send" /> <TextView android:id="@+id/textin" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> 

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.exercise.AndroidClient" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.INTERNET"/> <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MyClientActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

I get FatalException on client.pls, offering me how to handle this. I tried my IP address and 127.0.0.1, so I have any problems with the ip address or something else, because FatalException pls helps me.

logcat is as follows

 02-21 21:05:48.793: D/gralloc_goldfish(553): Emulator without GPU emulation detected. 02-21 21:08:39.794: D/AndroidRuntime(553): Shutting down VM 02-21 21:08:39.794: W/dalvikvm(553): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 02-21 21:08:39.824: E/AndroidRuntime(553): FATAL EXCEPTION: main 02-21 21:08:39.824: E/AndroidRuntime(553): android.os.NetworkOnMainThreadException 02-21 21:08:39.824: E/AndroidRuntime(553): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 02-21 21:08:39.824: E/AndroidRuntime(553): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 02-21 21:08:39.824: E/AndroidRuntime(553): at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 02-21 21:08:39.824: E/AndroidRuntime(553): at libcore.io.IoBridge.connect(IoBridge.java:112) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.Socket.startupSocket(Socket.java:566) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.Socket.tryAllAddresses(Socket.java:127) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.Socket.<init>(Socket.java:177) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.Socket.<init>(Socket.java:149) 02-21 21:08:39.824: E/AndroidRuntime(553): at com.exercise.AndroidClient.MyClientActivity$1.onClick(MyClientActivity.java:44) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.view.View.performClick(View.java:3511) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.view.View$PerformClick.run(View.java:14105) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.os.Handler.handleCallback(Handler.java:605) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:92) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:137) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4424) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:511) 02-21 21:08:39.824: E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 02-21 21:08:39.824: E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 02-21 21:08:39.824: E/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method) 
+1
source share
1 answer

You are using the network in the main application. This is not allowed in Android 3+. You need to run a separate stream access network.

You can use AsyncTask to split your code that activates the network into another Thread .

+6
source

Source: https://habr.com/ru/post/909435/


All Articles