|
![](http://forum.exceedu.com/forum/data/attachment/common/cf/020535hd92b49124hq644z.jpg) 我想用android手机连接服务器,但是运行到创建socket就直接跳出抛出异常请大家帮我我看看,代码如下:- package TCP.Isoso;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import java.io.OutputStream;
- import java.net.Socket;
- public class TCPTestActivity extends Activity {
-
- /** Called when the activity is first created. */
- TextView textview;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- textview = (TextView) findViewById(R.id.Textview1);
- Button sendMsg = (Button)findViewById(R.id.button1);
- sendMsg.setOnClickListener((OnClickListener) new MyButton());
- }
- class MyButton implements OnClickListener {
- public void onClick(View v)
- {
- Socket socket=null;
- try {
- socket = new Socket("118.244.232.138",8086);
- textview.setText("Creat!");
- OutputStream out = socket.getOutputStream();
- String buffer = "1004#121.553256, 38.931346|0.23*";
- out.write(buffer.getBytes(),0,64);
- textview.setText("OK!");
-
- } catch (Exception e) {
- e.printStackTrace();
- textview.setText("ERR");
- }
- }
- }
- }
复制代码 在虚拟机上执行就会显示“ERR”不知道为什么,查看网上有人说socket要建在非主线程上。但是也有的代码我看就这么做的~~到底错在哪里呢? |
|