Android Socket开发(客户端版)

 public class MainActivity extends Activity
{

	private StringBuilder sb;
	private static final String webName = "13482405***";
	private static final String token = "8b97642c42114b198190377aafe59463";
	
	private static final String HOST = "*****.com";
	private static final int PORT = 14100;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// 请求参数:00BM                     132000000050cf668b854de49ef937899d212e4bfce
		sb = new StringBuilder();
        sb.append("00BM");
        if (webName.length() > 32)
            sb.append(webName.substring(0, 32));
        else
            sb.append("                                ".substring(0, 32 - webName.length()) + webName);
        sb.append(token);
        
        // 
        new Thread()
        {
        	public void run() 
        	{
        		
        		try 
    			{
    				// 连接
    				Socket socket = new Socket(HOST, PORT);

    				// 写出(发送请求)
    				DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    				dos.writeBytes(sb.toString());

    				// 读入(读取返回消息)
    				BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    				String str = br.readLine();
    				Log.e("TAG", str);
    				
    				// 关闭
    				br.close();
    				dos.close();
    				socket.close();
    			} 
    			catch (Exception e) 
    			{
    				e.printStackTrace();
    			}
        		
        	};
        }.start();
	}

}

Android Socket开发(客户端版)

上一篇:关于MFC框架程序中CWinApp::OnIdle


下一篇:搭建eclipse android开发环境的一点记录