馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊  
 
x
 
  我們今天就來說說android怎麼樣才能和PC通信,這個我們就要用到socket了,那麼我們現在就來用一個小例子來看看。在代碼中我都用了註釋,希望大家來看看我發的帖子。 
 
java代碼:java代碼:- package eoe.demo;
 
  
- import java.io.BufferedReader;
 
 - import java.io.BufferedWriter;
 
 - import java.io.IOException;
 
 - import java.net.UnknownHostException;
 
  
- import java.io.InputStreamReader;
 
 - import java.io.OutputStreamWriter;
 
 - import java.net.Socket;
 
 - import android.app.Activity;
 
 - import android.os.Bundle;
 
 - import android.view.View;
 
 - import android.widget.*;
 
  
 
- public class YaoChatRoomAndroid extends Activity {
 
 - /** Called when the activity is first created. */
 
 - @Override
 
 - public void onCreate(Bundle savedInstanceState) {
 
 - super.onCreate(savedInstanceState);
 
 - setContentView(R.layout.main);
 
  
 
- findviews();
 
 - setonclick();
 
  
- }
 
  
- private EditText chattxt;
 
 - private TextView chattxt2;
 
 - private Button chatok;
 
  
- public void findviews()
 
 - {
 
 - chattxt = (EditText)this.findViewById(R.id.chattxt);
 
 - chattxt2 = (TextView)this.findViewById(R.id.chattxt2);
 
 - chatok = (Button)this.findViewById(R.id.chatOk);
 
 - }
 
  
- private void setonclick()
 
 - {
 
 - chatok.setOnClickListener(new View.OnClickListener() {
 
  
- @Override
 
 - public void onClick(View v) {
 
 - try {
 
 - connecttoserver(chattxt.getText().toString());
 
 - } catch (UnknownHostException e) {
 
 - // TODO Auto-generated catch block
 
 - e.printStackTrace();
 
 - } catch (IOException e) {
 
 - // TODO Auto-generated catch block
 
 - e.printStackTrace();
 
 - }
 
 - }
 
 - });
 
 - }
 
  
 
- public void connecttoserver(String socketData) throws UnknownHostException, IOException
 
 - {
 
 - Socket socket=RequestSocket("192.168.10.119",5000);
 
 - SendMsg(socket,socketData); 
 
 - String txt = ReceiveMsg(socket);
 
 - this.chattxt2.setText(txt); 
 
 - }
 
  
 
- private Socket RequestSocket(String host,int port) throws UnknownHostException, IOException
 
 - { 
 
 - Socket socket = new Socket(host, port);
 
 - return socket;
 
 - }
 
  
- private void SendMsg(Socket socket,String msg) throws IOException
 
 - {
 
 - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
 
 - writer.write(msg.replace("\n", " ")+"\n");
 
 - writer.flush();
 
 - }
 
  
- private String ReceiveMsg(Socket socket) throws IOException
 
 - {
 
 - BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
 
  
- String txt=reader.readLine();
 
 - return txt;
 
  
- } 
 
 - }
 
  複製代碼 java代碼:- <?xml version="1.0" encoding="utf-8"?>
 
 - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
 - android:orientation="vertical"
 
 - android:layout_width="fill_parent"
 
 - android:layout_height="fill_parent"
 
 - >
 
 - <TextView
 
 - android:id="@+id/chattxt2"
 
 - android:layout_width="319px"
 
 - android:layout_height="68px"
 
 - android:text="TextView"
 
 - android:layout_alignParentTop="true"
 
 - android:layout_alignParentLeft="true"
 
 - >
 
 - </TextView>
 
 - <EditText
 
 - android:id="@+id/chattxt"
 
 - android:layout_width="319px"
 
 - android:layout_height="52px"
 
 - android:text="EditText"
 
 - android:textSize="18sp"
 
 - android:layout_below="@+id/widget30"
 
 - android:layout_alignParentLeft="true"
 
 - >
 
 - </EditText>
 
 - <Button
 
 - android:id="@+id/chatOk"
 
 - android:layout_width="320px"
 
 - android:layout_height="41px"
 
 - android:text="Button"
 
 - android:layout_below="@+id/widget29"
 
 - android:layout_alignParentLeft="true"
 
 - >
 
 - </Button>
 
  
- </LinearLayout>
 
  複製代碼 |