Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Socket開發(客戶端版)

Android Socket開發(客戶端版)

編輯:關於Android編程

 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();
	}

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved