title: App fails to connect to a simple server
body: <p>I've made a simple chat app for desktop use in Java along with a server. Now, I rebuilt the desktop app into an android one. The only problem is I can't get it to connect to the server. It didn't work in the background so I tried with a button but still no result.</p>
  
  <p>I am running the server in eclipse and the app in an android studio with an emulator.</p>
  
  <p>IMPORTANT NOTE: It shows error:</p>
  
  <pre><code>08-01 18:00:57.255 8914-8914/com.example.android.chatnow E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.android.chatnow, PID: 8914
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.chatnow/com.example.android.chatnow.MainActivity}: android.os.NetworkOnMainThreadException
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
          at android.app.ActivityThread.-wrap11(Unknown Source:0)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6494)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
       Caused by: android.os.NetworkOnMainThreadException
          at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)
          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:357)
          at java.net.Socket.connect(Socket.java:616)
          at java.net.Socket.connect(Socket.java:565)
          at java.net.Socket.&lt;init&gt;(Socket.java:445)
          at java.net.Socket.&lt;init&gt;(Socket.java:217)
          at com.example.android.chatnow.MainActivity.setUpNetworking(MainActivity.java:29)
          at com.example.android.chatnow.MainActivity.onCreate(MainActivity.java:17)
          at android.app.Activity.performCreate(Activity.java:7009)
          at android.app.Activity.performCreate(Activity.java:7000)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
          at android.app.ActivityThread.-wrap11(Unknown Source:0) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:164) 
          at android.app.ActivityThread.main(ActivityThread.java:6494) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>package com.example.android.chatnow;
  import java.io.*;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.TextView;
  
  import java.net.*;
  public class MainActivity extends AppCompatActivity {
      BufferedReader reader;
      PrintWriter writer;
      Socket sock;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Thread readerThread = new Thread(new IncomingReader());
          readerThread.start();
      }
      public void onSendMessage(View view) {
          EditText messageView = (EditText)findViewById(R.id.message);
          String messageText = messageView.getText().toString();
          //writer.println(": " +messageText);
          //writer.flush();
      }
      public void setUpNetworking(View view) {
          try {
              sock=new Socket("10.0.2.2", 5000);
              InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
              reader = new BufferedReader(streamReader);
              writer = new PrintWriter(sock.getOutputStream());
              Log.d("NiceTag", "Net established");
          }catch (Exception ex){
              ex.printStackTrace();
              Log.d("NiceTag", "Net failed");
          }
      }
      public class IncomingReader implements Runnable{
          public void run() {
              String message;
              try {
                  while ( (message=reader.readLine())!=null) {
                      TextView messageView = (TextView)findViewById(R.id.displaymessage);
                      messageView.append( message + '\n');
                  }
                  System.out.println("!while");
              } catch(Exception ex) {
                  ex.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>And the XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"&gt;
  
      &lt;TextView
          android:id="@+id/displaymessage"
          android:layout_width="262dp"
          android:layout_height="401dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
      &lt;EditText
          android:id="@+id/message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="16dp"
          android:layout_marginTop="40dp"
          android:ems="10"
          android:inputType="textPersonName"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/displaymessage" /&gt;
  
      &lt;Button
          android:id="@+id/sendbutton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginEnd="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="36dp"
          android:text="Send"
          android:onClick="onSendMessage"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.469"
          app:layout_constraintStart_toEndOf="@+id/message"
          app:layout_constraintTop_toBottomOf="@+id/displaymessage" /&gt;
  
      &lt;Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginEnd="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:text="Connect"
          android:onClick="setUpNetworking"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toEndOf="@+id/displaymessage"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>Also, the manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.android.chatnow"&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;application
  
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>P.S. Server and emulator are running on the same device</p>