由於最近實驗室項目要求實如今局域網下將android app數據發送到winsock中進行保存,因此對此進行了簡單學習。pc端由於是另外一個同窗作的,因此不作說明。java
在android端,首先添加權限:android
<uses-permission android:name="android.permission.INTERNET"/>
以後對app界面進行簡單設計——main.xml:服務器
<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 定義一個文本框,它用於接收用戶的輸入 --> <EditText android:id="@+id/input" android:layout_width="280dp" android:layout_height="wrap_content" android:inputType=""/> <Button android:id="@+id/send" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingStart="8dp" android:text="@string/send"/> </LinearLayout> <!-- 定義一個文本框,它用於顯示來自服務器的信息 --> <TextView android:id="@+id/show" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top" android:background="#ffff" android:textSize="14sp" android:textColor="#f000"/>
界面以下:網絡
建立ClientThread.java用於實現局域網下數據的傳輸:app
public class ClientThread implements Runnable { private Socket s; // 定義向UI線程發送消息的Handler對象 private Handler handler; // 定義接收UI線程的消息的Handler對象 public Handler revHandler; // 該線程所處理的Socket所對應的輸入流 BufferedReader br = null; OutputStream os = null; public ClientThread(Handler handler) { this.handler = handler; } public void run() { try { s = new Socket("192.168.1.133", 8040); br = new BufferedReader(new InputStreamReader( s.getInputStream())); os = s.getOutputStream(); // 啓動一條子線程來讀取服務器響應的數據 new Thread() { @Override public void run() { String content = null; // 不斷讀取Socket輸入流中的內容 try { while ((content = br.readLine()) != null) { // 每當讀到來自服務器的數據以後,發送消息通知程序 // 界面顯示該數據 Message msg = new Message(); msg.what = 0x123; msg.obj = content.toString(); handler.sendMessage(msg); } } catch (IOException e) { e.printStackTrace(); } } }.start(); // 爲當前線程初始化Looper Looper.prepare(); // 建立revHandler對象 revHandler = new Handler() { @Override public void handleMessage(Message msg) { // 接收到UI線程中用戶輸入的數據 if (msg.what == 0x345) { // 將用戶在文本框內輸入的內容寫入網絡 try { os.write((msg.obj.toString() + "\r\n") .getBytes("utf-8")); } catch (Exception e) { e.printStackTrace(); } } } }; // 啓動Looper Looper.loop(); } catch (SocketTimeoutException e1) { System.out.println("網絡鏈接超時!!"); } catch (Exception e) { e.printStackTrace(); } } }
最後建立MainActivity.java實現app中發送內容的獲取等功能:ide
public class MainActivity extends Activity { // 定義界面上的兩個文本框 EditText input; TextView show; // 定義界面上的一個按鈕 Button send; Handler handler; // 定義與服務器通訊的子線程 ClientThread clientThread; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); input = (EditText) findViewById(R.id.input); send = (Button) findViewById(R.id.send); show = (TextView) findViewById(R.id.show); handler = new Handler() // ② { @Override public void handleMessage(Message msg) { // 若是消息來自於子線程 if (msg.what == 0x123) { // 將讀取的內容追加顯示在文本框中 show.append("\n" + msg.obj.toString()); } } }; clientThread = new ClientThread(handler); // 客戶端啓動ClientThread線程建立網絡鏈接、讀取來自服務器的數據 new Thread(clientThread).start(); // ① send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { // 當用戶按下發送按鈕後,將用戶輸入的數據封裝成Message // 而後發送給子線程的Handler Message msg = new Message(); msg.what = 0x345; msg.obj = input.getText().toString(); clientThread.revHandler.sendMessage(msg); // 清空input文本框 input.setText(""); } catch (Exception e) { e.printStackTrace(); } } }); } }
這樣,就實現了咱們想要的傳輸數據到pc中的功能了。oop