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(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。