你好,游客 登录 注册 搜索
背景:
阅读新闻

Socket在Android中的应用

[日期:2012-04-04] 来源:Linux社区  作者:郝浩 [字体:大 中 小]

客服端代码:

  1. package com.Aina.Android;  
  2. import java.io.BufferedReader;  
  3. import java.io.BufferedWriter;  
  4. import java.io.InputStreamReader;  
  5. import java.io.OutputStreamWriter;  
  6. import java.io.PrintWriter;  
  7. import java.net.Socket;  
  8. import android.app.Activity;  
  9. import android.app.AlertDialog;  
  10. import android.content.DialogInterface;  
  11. import android.os.Bundle;  
  12. import android.os.Handler;  
  13. import android.os.Message;  
  14. import android.util.Log;  
  15. import android.view.View;  
  16. import android.widget.Button;  
  17. import android.widget.EditText;  
  18. import android.widget.TextView;  
  19. public class Test extends Activity implements Runnable {  
  20. /** Called when the activity is first created. */  
  21. private TextView tv_msg = null;  
  22. private EditText ed_msg = null;  
  23. private Button btn_send = null;  
  24. private Button btn_login = null;  
  25. private static final String HOST = "192.168.0.132";  
  26. private static final int PORT = 9999;  
  27. private Socket socket = null;  
  28. private BufferedReader in = null;  
  29. private PrintWriter out = null;  
  30. private String content = "";  
  31. @Override  
  32. public void onCreate(Bundle savedInstanceState) {  
  33.   super.onCreate(savedInstanceState);  
  34.   setContentView(R.layout.main);  
  35.   tv_msg = (TextView) this.findViewById(R.id.TextView);  
  36.   ed_msg = (EditText) this.findViewById(R.id.EditText01);  
  37.   btn_login = (Button) this.findViewById(R.id.Button01);  
  38.   btn_send = (Button) this.findViewById(R.id.Button02);  
  39.   try {  
  40.    socket = new Socket(HOST, PORT);  
  41.    in = new BufferedReader(new InputStreamReader(socket  
  42.      .getInputStream()));  
  43.    out = new PrintWriter(new BufferedWriter(  
  44.      new OutputStreamWriter(socket.getOutputStream())),  
  45.      true);  
  46.   } catch (Exception ex) {  
  47.    ex.printStackTrace();  
  48.    ShowDialog("登陆异常:" + ex.getMessage());  
  49.   }  
  50.   btn_send.setOnClickListener(new Button.OnClickListener() {  
  51.    public void onClick(View v) {  
  52.     // TODO Auto-generated method stub   
  53.     String msg = ed_msg.getText().toString();  
  54.     if (socket.isConnected()) {  
  55.      if (!socket.isOutputShutdown()) {  
  56.       out.println(msg);  
  57.      }  
  58.     }  
  59.    }  
  60.   });  
  61.   new Thread(this).start();  
  62. }  
  63. public void ShowDialog(String msg) {  
  64.   new AlertDialog.Builder(this).setTitle("提示").setMessage(msg)  
  65.     .setPositiveButton("OK", new DialogInterface.OnClickListener() {  
  66.      public void onClick(DialogInterface dialog, int which) {  
  67.       // TODO Auto-generated method stub   
  68.      }  
  69.     }).show();  
  70. }  
  71. public void run() {  
  72.   try {  
  73.    while (true) {  
  74.     if(socket.isConnected()){  
  75.      if(!socket.isInputShutdown()){  
  76.       if ((content = in.readLine()) != null) {  
  77.        Log.i("TAG", "++ "+content);  
  78.        content += "\n";  
  79.        mHandler.sendMessage(mHandler.obtainMessage());  
  80.       }else{  
  81.         
  82.       }  
  83.      }  
  84.     }  
  85.       
  86.    }  
  87.   } catch (Exception ex) {  
  88.    ex.printStackTrace();  
  89.   }  
  90. }  
  91. public Handler mHandler = new Handler() {  
  92.   public void handleMessage(Message msg) {  
  93.    super.handleMessage(msg);  
  94.    Log.i("TAG", "-- "+msg);  
  95.    tv_msg.setText(tv_msg.getText().toString() + content);  
  96.   }  
  97. };  
  98. }  
linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款