簡單學習Android Socket的使用方法_Android

來源:互聯網
上載者:User

這方面的知識不是孤立的,其中有關於,Socket編程,多線程的操作,以及I/O流的操作。當然,實現方法不止一種,這隻是其中一種,給同是新手一點點思路。如果有什麼推薦的話,歡迎指點!
先給大家看一下應用程式的介面,基本就能知道大致的功能了。

 

activity_main.java

<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" tools:context=".MainActivity"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入要發送的內容"/>  <Button android:id="@+id/button01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="串連"/> <Button android:id="@+id/button02" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="發送"/> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:fadingEdge="vertical"> <TextView  android:id="@+id/textView"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="輸出資訊:"/> </ScrollView></LinearLayout>

介面很是簡單。

下面我們需要一個伺服器,和一個用戶端。伺服器,我用的是Eclipse寫的Java的伺服器;用戶端,我用的是Android Studio寫的。

package com.ryan.socketdemo01;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import java.io.IOException;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.Socket;/** * 本執行個體功能: 用戶端發送資料至用戶端(動態輸出資料) * */public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button button01 = null; private Button button02 = null; private EditText editText = null; private TextView textView = null; private static Socket ClientSocket = null; private byte[] msgBuffer = null; Handler handler = new Handler(); private void initView() { button01 = (Button) findViewById(R.id.button01); button02 = (Button) findViewById(R.id.button02); editText = (EditText) findViewById(R.id.editText); textView = (TextView) findViewById(R.id.textView); button01.setOnClickListener(this); button02.setOnClickListener(this); button01.setEnabled(true); button02.setEnabled(false); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } @Override public void onClick(View v) { switch (v.getId()){  case R.id.button01:  // TODO: 15-9-4 socket連接線程  connectThread();  break;  case R.id.button02:  // TODO: 15-9-4 發送資料線程  sendMsgThread();  break; } } private void sendMsgThread() { final String text = editText.getText().toString(); try {  msgBuffer = text.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) {  e.printStackTrace(); } new Thread(new Runnable() {  @Override  public void run() {  try {   OutputStream outputStream;   //Socket輸出資料流   outputStream = ClientSocket.getOutputStream();   outputStream.write(msgBuffer);   outputStream.flush();   outputStream.close();  } catch (IOException e) {   e.printStackTrace();  }  handler.post(new Runnable() {   @Override   public void run() {   textView.append("發送成功:"+text+"\n");   }  });  } }).start(); } private void connectThread() { new Thread(new Runnable() {  @Override  public void run() {  try {   ClientSocket = new Socket("10.0.2.2",9001);   if (ClientSocket.isConnected()){   handler.post(new Runnable() {    @Override    public void run() {    textView.append("串連成功!"+"\n");    button01.setEnabled(false);    button02.setEnabled(true);    }   });   }else {   handler.post(new Runnable() {    @Override    public void run() {    textView.append("串連失敗!"+"\n");    }   });   }  } catch (IOException e) {   e.printStackTrace();  }  } }).start(); }
 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) {  return true; } return super.onOptionsItemSelected(item); }}

這裡我的線程使用方式是:

new Thread (new Runnable) { @Override public void run() { } } 

  網上有人說這個方式很LOW,而且不好,但現在我只會這個,就連asynctask也還在學習中。 

還有一點,子線程更新主UI的方法:

我使用的是 Handler.post();  同樣十分簡單的使用方法。

Handler handler = new Handler(); handler.post(new Runnable() {   @Override   public void run() {   textView.append("發送成功:"+text+"\n");   }  }); 

關於幾種子線程更新主UI的方法,我以後會再寫一篇部落格。我現在已經知道了不下4中方法,還沒實地操作。 

再來就是關於I/O流操作的方法:

這裡我不做詳細介紹,大家自行Google!什麼你不會Google? 自行找FQ工具吧~~

這裡我說一下我對於I/O的簡單使用方法。

萬物始於最初的,InputStream,OutputStream,他的方法只有reader()和write()。再其之上的,例如什麼使用最多的BufferedReader對象,都是再其之上的升級封裝,穿了個衣服,變得更加華麗而已。他的專業術語就是——裝飾者模式,感興趣的可以去翻翻資料。

最初,我對InputStream和OutputStream的方法不是很明了,經常錯將InputStream用上write()的方法。 不知在哪裡看到這麼一段話,I/O流的操作實質是相對於Socket,ServerSocket串連後產生的資料流管道,reader與write是讀取與寫入的意思,是相對於那個資料流管道進行操作的。即讀取管道裡的資訊,寫入資訊至管道。不知道這樣大家動不動。 

最後就是我們要學習的Socket。

他的使用也很簡單,建立一個Socket對象,設定IP和連接埠,擷取其I/O流。

ClientSocket = new Socket("10.0.2.2",9001);outputStream = ClientSocket.getOutputStream();

到這裡基本的使用方法就這麼多。

說說我在這其中遇到的問題:

1.模擬器串連Java伺服器串連不上。IP設定不對,我最開始設定的Ip是 127.0.0.1。 解決的串連:

http://stackoverflow.com/questions/8191192/reaching-a-network-device-by-ip-and-port-using-the-android-emulator/8191487#8191487

http://stackoverflow.com/questions/10336637/how-do-i-socket-an-android-program-to-a-localhost-server

 2.忘記設定manifest,或者系統提供的android.permission.INTERNET全是大寫的!!說來滑稽,但我就是遇到這樣的問題,系統自動完成的代碼提示,全是大寫的,我以為就是那樣呢,結果明明應該是小寫。

<uses-permission android:name="android.permission.INTERNET" />

忘記貼上java伺服器的代碼了,補上。

/** * 本執行個體功能:接受伺服器發來的資料 */public class Main {  public static void main(String[] args) {    // TODO Auto-generated method stub    new SerVerListener().start();  }}

 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.ServerSocket;import java.net.Socket;import javax.swing.JOptionPane;public class SerVerListener extends Thread{    private Socket clientSocket = null;  private ServerSocket serverSocket = null;  private InputStream inputStream = null;  private byte[] buffer = null;    @Override  public void run() {    // TODO Auto-generated method stub        try {      serverSocket = new ServerSocket(9001);      System.out.println("連接埠已開啟,等待串連中〜〜");      //block      clientSocket = serverSocket.accept();      System.out.println("已有使用者串連");      inputStream = clientSocket.getInputStream();            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));      String str;            while((str = br.readLine())!= null){        System.out.println(str);      }          }catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();    }finally{      try {        inputStream.close();        serverSocket.close();      } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();      }    }          }}

這是伺服器端就收到的資料:

 

 

OK,基本就這樣。

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.