Home > Mobile >  Run on the simulator run no problem, the real machine is the Exception
Run on the simulator run no problem, the real machine is the Exception

Time:10-24

Get the socket on the local network communication, running in the simulator to communicate everything in good condition, but really running on the phone, will collapse,
All function running normally, simulator can be normal to send information, on the real machine can connect to the server, but as soon as I send information to collapse, it throws an exception, the program will shut down
AS prompt information is
 E/AndroidRuntime: FATAL EXCEPTION: the main 
Process: com. Example. Inchat, PID: 2857
Android.os.Net workOnMainThreadException
. An android OS. StrictMode $AndroidBlockGuardPolicy. OnNetwork (StrictMode. Java: 1303)
The at java.net.SocketOutputStream.socketWrite (SocketOutputStream that Java: 111)
The at java.net.SocketOutputStream.write (SocketOutputStream that Java: 157)
The at Java. IO. PrintStream. Write (PrintStream. Java: 492)
At sun. Nio. Cs. StreamEncoder. WriteBytes (StreamEncoder. Java: 221)
At sun. Nio. Cs. StreamEncoder. ImplFlushBuffer (StreamEncoder. Java: 291)
At sun. Nio. Cs. StreamEncoder. FlushBuffer (StreamEncoder. Java: 104)
The at Java. IO. OutputStreamWriter. FlushBuffer (OutputStreamWriter. Java: 185)
The at Java. IO. PrintStream. Write (PrintStream. Java: 543)
The at Java. IO. PrintStream. Print (PrintStream. Java: 687)
The at Java. IO. PrintStream. Println (PrintStream. Java: 824)
At com. Example. Inchat. MainActivity $MsgThread $1. The onClick (MainActivity. Java: 120)
An android. View. The view. PerformClick (the Java: 5647)
An android. View. The view $PerformClick. Run (22461) view. Java:
An android. OS. Handler. HandleCallback (Handler. Java: 754)
An android. OS. Handler. DispatchMessage (Handler. Java: 95)
. An android OS. Stars. Loop (163). Which Java:
. An android app. ActivityThread. Main (6374) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
At com. Android. Internal. OS. $MethodAndArgsCaller ZygoteInit. Run (ZygoteInit. Java: 931)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 821)


On the server side tip information is


 java.net.SocketException: Connection reset 
The at java.net.SocketInputStream.read SocketInputStream. Java: (210)
The at java.net.SocketInputStream.read SocketInputStream. Java: (141)
At sun. Nio. Cs. StreamDecoder. ReadBytes (StreamDecoder. Java: 284)
At sun. Nio. Cs. StreamDecoder. ImplRead (StreamDecoder. Java: 326)
At sun. Nio. Cs. StreamDecoder. Read (178) StreamDecoder. Java:
The at Java. IO. InputStreamReader. Read (InputStreamReader. Java: 184)
The at Java. IO. BufferedReader. The fill (BufferedReader. Java: 161)
The at Java. IO. BufferedReader readLine (BufferedReader. Java: 324)
The at Java. IO. BufferedReader readLine (BufferedReader. Java: 389)
At Server $Service. The run (for Server Java: 72)
The at Java. Util. Concurrent. ThreadPoolExecutor. RunWorker (ThreadPoolExecutor. Java: 1149)
The at Java. Util. Concurrent. ThreadPoolExecutor $Worker. The run (ThreadPoolExecutor. Java: 624)
The at Java. Lang. Thread. The run (Thread. Java: 748)


The code
 package com. Example. Inchat; 

The import android. App. AlertDialog;
Import android. Content. DialogInterface.
The import android. Content. Intent;
The import android. OS. Stars;
The import android. Support. V7. App. AppCompatActivity;
The import android. OS. Bundle;
The import android. View. The view;
The import android. Widget. The Button;
The import android. Widget. The EditText;
The import android. Widget. TextView;
The import android. Widget. ThemedSpinnerAdapter;
The import android. Widget. Toast;

Import the Java. IO. BufferedReader;
Import the Java. IO. BufferedWriter;
Import the Java. IO. IOException;
Import the Java. IO. InputStreamReader;
Import the Java. IO. OutputStreamWriter;
Import the Java. IO. PrintStream;
Import the Java. IO. PrintWriter;
The import java.net.Socket;

Public class MainActivity extends AppCompatActivity {
Private String host="192.168.0.107";
Private Button Button;
Private EditText EditText;
Private TextView TextView;
Private StringBuilder sb=null;
Private BufferedReader br;
Private PrintStream ps;
Private PrintWriter pw.
Private Intent Intent;
Public static final int port=12345;
int count=0;
The Socket client;
String s;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
setContentView(R.layout.activity_main);
The button=(button) the findViewById (R.i db utton_send);
The editText=(editText) the findViewById (R.i d.e ditText);
TextView=(textView) the findViewById (R.i which extView);

ShowInputDialog ();

}

//# 99 CCFF

Private class DialogThread implements Runnable {
Public DialogThread () {
New Thread (this). The start ();
}
@ Override
Public void the run () {
ShowInputDialog ();
}
}


Public void showInputDialog () {
Final Intent Intent=new Intent ();
Final EditText editText1=new EditText (MainActivity. This);
AlertDialog. Builder inputDialog=new AlertDialog. Builder (MainActivity. This);
InputDialog. SetTitle (" a nice point name ");
InputDialog. SetView (editText1);
The final String [] t=new String [1].
InputDialog. SetPositiveButton (" sure, "the new DialogInterface. An OnClickListener () {

@ Override
Public void onClick (DialogInterface dialog, int which) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related