android socket编程client
来源:互联网 发布:世界杯门票 知乎 编辑:程序博客网 时间:2024/05/21 17:56
一个android通信的客户端实例,力求代码简洁,程序完善。
复制粘贴之后可直接运行
activity篇:
package com.mysocket.androidclient;import java.io.*;import java.net.InetSocketAddress;import java.net.Socket;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity { private Socket mSocket; private OutputStream mOut; private TextView mMsgView = null; //发送信息和接收信息的文本框 private EditText mMsgSend = null; //定义发送信息的文本框 private Button mBtnConnect = null; //定义一个连接按钮 private Button mBtnSend = null; //定义发送信息的按钮 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得UI的实例 mMsgView = (TextView) findViewById(R.id.textView_socketMsg); mBtnConnect = (Button) findViewById(R.id.btn_connect); mBtnSend = (Button) findViewById(R.id.btn_send); mMsgSend = (EditText) findViewById(R.id.editText_send); //点击“连接”按钮,建立socket连接 mBtnConnect.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new Thread(new MyClientThread()).start(); mBtnConnect.setText("已 连 接"); } }); //发送信息 mBtnSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { String sendStr = mMsgSend.getText().toString(); mOut.write((sendStr + "\n").getBytes()); mMsgSend.setText(""); Message send = new Message(); send.obj = "客户端:\n\t\t" + sendStr; mHandler.sendMessage(send); } catch (IOException e) { e.printStackTrace(); } } }); } public Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); mMsgView.append(msg.obj.toString() + "\n"); } }; class MyClientThread implements Runnable { @Override public void run() { try { // 建立socket连接,获取输入输出流 mSocket = new Socket(); mSocket.connect(new InetSocketAddress("113.251.218.119", 8080), 50000); mOut = mSocket.getOutputStream(); BufferedReader in = new BufferedReader(new InputStreamReader( mSocket.getInputStream())); // 读取发来服务器信息 String line = null; while ((line = in.readLine()) != null) { Message reveived = new Message(); reveived.obj = "服务器端:\n\t" + line; mHandler.sendMessage(reveived); } // 关闭各种输入输出流 in.close(); mOut.close(); mSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/editText_send" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textMultiLine" > <requestFocus /> </EditText> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" android:text="发 送" /> </LinearLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:id="@+id/textView_socketMsg" android:layout_width="match_parent" android:layout_height="wrap_content" android:isScrollContainer="true" android:text="" /> </ScrollView> <Button android:layout_weight="0" android:id="@+id/btn_connect" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点 击 连 接" /></LinearLayout>
最后,别忘了加访问网络的权限
<uses-permission android:name="android.permission.INTERNET"/>
0 0
- android socket编程client
- Java Socket 编程 Client
- linux socket编程-client
- Java Socket Client编程
- Java编程 之Socket Client
- socket client 编程之一:入门
- java socket 编程(Client)
- IOS编程Socket Client编写
- JAVA 网络编程 Socket server client 编程
- Android-socket-client 客户端实现
- Android-socket-client客户端<2>
- coro::socket+coro::handle client编程(测试client)
- TCP Socket编程实例1---Client端
- Socket网络编程之Client端
- Socket编程—Client、Server通信
- Java----Socket编程之模拟客户端Client
- Socket.io-client android 部分源码解析
- Android Java端的Socket.io-client
- Android虚拟机启动后,设备视图无设备。运行项目提示Please ensure that adb is correctly located at …问题的解决方法
- 9
- $Proxy11 cannot be cast to cn.oa.service.impl.UserServiceImpl
- VNC远程登录树莓派的图形界面
- Python yield 用法
- android socket编程client
- thinkpython 练习题4.3
- hdu3642 Get The Treasury
- Oracle结构化查询语言(Structured Query Language)
- Web framework之Mybatis3
- golang udp
- 杭电 HDU ACM 1330 Deck
- 10
- Hiberate之数据对象关联关系