日期:2014-05-20 浏览次数:25383 次
package cn.bzu.edu.Thread; import android.app.Activity; import android.graphics.Canvas; import android.os.Bundle; import android.widget.TextView; public class THREAD_1_ACTIVITY extends Activity { /** Called when the activity is first created. */ int x=1; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)findViewById(R.id.myTextview); AndroidThread thread=new AndroidThread(tv); } } //AndroidThread类 package cn.bzu.edu.Thread; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import android.widget.TextView; public class AndroidThread implements Runnable { TextView myTextView; Socket socket; DataInputStream dis=null; DataOutputStream dos=null; public AndroidThread(TextView tv) { // TODO Auto-generated constructor stub myTextView = tv; Thread thread=new Thread(this); socket=new Socket(); try { InetAddress address=InetAddress.getByName("10.3.5.136"); InetSocketAddress socketAddress=new InetSocketAddress(address,6666); socket.connect(socketAddress); dis=new DataInputStream(socket.getInputStream()); dos=new DataOutputStream(socket.getOutputStream()); } catch (UnknownHostException e) { tv.setText("socket"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } thread.start(); } @Override public void run() { // TODO Auto-generated method stub String s; try { //myTextView.setText("execute run method()"); if(socket.isConnected()){ if(socket!=null){ dos.writeUTF("POST"); myTextView.setText("post"); s=dis.readUTF(); myTextView.setText(s); }else { myTextView.setText("socket is null"); } }else { myTextView.setText("socket NOT CONNECTION"); } } catch (Exception e) { // TODO Auto-generated catch block } } }