java NetworkOnMainThreadException错误
运行线路时
echoSocket = new Socket(serverHostname, 10008);
我的应用程序因错误而崩溃,NetworkOnMainThreadException错误
据我所见,这与尝试在UI线程上运行socket有关。那么,我如何更改我的代码(如下)使其工作
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.support.v7.app.ActionBar;
import 安卓.support.v4.app.Fragment;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
import 安卓.os.Build;
public class MainActivity extends ActionBarActivity {
Button Bgo;
private static TextView TV1;
EditText Text;
String userInput;
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
String Responce = null;
String serverHostname;
int section = 0;
Boolean keepgoing = true;
Boolean g1o = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bgo = (Button) findViewById(R.id.Go);
TV1 = (TextView) findViewById(R.id.tv1);
Text = (EditText) findViewById(R.id.Inputtext);
onclick();
}
public void send() {
// System.out.println("type the host name"); ->>updating
// serverHostname = new String(stdIn.readLine());
serverHostname = new String("192.168.1.105");
System.out.println("Attemping to connect to host " + serverHostname
+ " on port 10008.");
try {
echoSocket = new Socket(serverHostname, 10008);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
TV1.setText("Don't know about host: " + serverHostname);
} catch (IOException e) {
TV1.setText("Couldn't get I/O for " + "the connection to: "
+ serverHostname);
}
TV1.setText("type VIEW-LOG for last 5 enteries (newset first) or DC to dissconect");
}
public void onclick() {
Bgo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (g1o = true) {
TV1.setText("conecting");
g1o = false;
send();
} else {
userInput = Text.getText().toString();
try {
if (userInput.equals("VIEW-LOG")) {
out.println(userInput);
int i = 0;
while (i < 5) {
Responce = in.readLine();
System.out.println(Responce);//this will be replace once this is working
i++;
}
} else if (userInput.equals("DC")) {
keepgoing = false;
System.out.println("dc");
out.println(userInput);
} else {
out.println(userInput);
System.out.println(in.readLine());
}
} catch (IOException e) {
}
}
}
});
}
public void Close() {
try {
out.close();
in.close();
echoSocket.close();
} catch (IOException e) {
}
}
}
XML:
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:orientation="vertical" >
<TextView
安卓:id="@+id/tv1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="PlaceHolder"
安卓:textAppearance="?安卓:attr/textAppearanceLarge" />
<EditText
安卓:id="@+id/Inputtext"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:ems="10" >
<requestFocus />
</EditText>
<Button
安卓:id="@+id/Go"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Go" />
</LinearLayout>
此剂量:按一个按钮即可运行send();连接到服务器的。再次按下时,它会将edittext中的测试发送到服务器,如果是DC,则会断开与服务器的连接,如果是VIEW-LOG,则服务器会发送5组字符串
# 1 楼答案
当您在主线程上执行android不允许执行的某些网络操作时,会引发此异常。将
send
方法调用移动到非UI线程,或者最好将代码移动到AsyncTask
# 2 楼答案
您不应该在主线程上执行任何与网络相关的操作
或者使用
Handler
http://developer.android.com/reference/android/os/Handler.html或
一个
AsyncTask
{a2}