練習android網絡知識。先介紹一下大概流程。首先是建立一個java工程,并建立兩個java類,一個用于接收到用戶端的連接配接,并把連接配接添加list中,第二類實作線程runnable接口,專門用來接收發送客戶發送的資訊。其次,建立android工程,并建立兩個類,一個用于顯示聊天界面,另一個負責接收伺服器端傳回的資訊。這個例子肯定會有考慮不周的地方但是隻是為了學習android中網絡相關api的使用,是以請大家謹慎拍磚。
首先還是android的内容
<RelativeLayout 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" >
<EditText
android:id="@+id/et_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="5"
android:hint="所有聊天資訊"
android:gravity="center"
/>
<EditText
android:id="@+id/et_input"
android:layout_below="@+id/et_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="輸入聊天資訊"
android:gravity="center"
/>
<Button
android:id="@+id/bt_send"
android:layout_below="@+id/et_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="發送資訊"
/>
< /RelativeLayout>
接着是MainAvitvity.java
[java]
public class MainActivity extends Activity {
private EditText et_show,et_input;
private Button bt_send;
private OutputStream os;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_input = (EditText) this.findViewById(R.id.et_input);
et_show = (EditText) this.findViewById(R.id.et_show);
bt_send = (Button) this.findViewById(R.id.bt_send);
try {
//定義客戶連接配接的socket
Socket socket = new Socket("本機IP",30000);
//啟動用戶端監聽線程
new Thread(new ClinetThread(socket,handler)).start();
os = socket.getOutputStream();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
bt_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
//得到輸入框中的内容,寫入到輸入流中
os.write((et_input.getText().toString()+"\r\n").getBytes("utf-8"));
et_input.setText("");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 1){
//得到伺服器傳回的資訊
et_show.append("\n"+msg.obj.toString());
}
}
};
}
}
第三是用戶端的線程類
[java]
public class ClinetThread implements Runnable{
Socket socket = null;
Handler handler = null;
BufferedReader br = null;
public ClinetThread(Socket socket,Handler handler) {
this.socket = socket;
this.handler = handler;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
String content = null;
try {
while((content = br.readLine())!=null){
Message msg = new Message();
msg.what = 1;
msg.obj = content;
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
接下來是Java工程中的主類
[java]
public class SimpleServer {
//定義儲存所有Socket的ArrayList
public static ArrayList<Socket> socketList = new ArrayList<Socket>();
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(30000);
while (true) {
Socket s = ss.accept();
socketList.add(s);
new Thread(new ServerThead(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
其次java工程中的線程類
[java]
public class ServerThead implements Runnable {
//定義目前線程處理的socket
Socket s = null;
//該線程所處理的socket對應的輸入流
BufferedReader br = null;
public ServerThead(Socket s) throws IOException {
this.s = s;
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
@Override
public void run() {
String conntent = null;
while((conntent=readFromClient())!=null){
//周遊socket中的每一個socket
for(Socket s:SimpleServer.socketList){
try {
OutputStream os = s.getOutputStream();
os.write((conntent+"\n").getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String readFromClient() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
SimpleServer.socketList.remove(s);
}
return null;
}
}