簡單學(xué)習(xí)Android Socket的使用方法
這方面的知識不是孤立的,其中有關(guān)于,Socket編程,多線程的操作,以及I/O流的操作。當(dāng)然,實現(xiàn)方法不止一種,這只是其中一種,給同是新手一點點思路。如果有什么推薦的話,歡迎指點!
先給大家看一下應(yīng)用程序的界面,基本就能知道大致的功能了。
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="請輸入要發(fā)送的內(nèi)容"/> <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="發(fā)送"/> <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>
界面很是簡單。
下面我們需要一個服務(wù)器,和一個客戶端。服務(wù)器,我用的是Eclipse寫的Java的服務(wù)器;客戶端,我用的是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; /** * 本實例功能: 客戶端發(fā)送數(shù)據(jù)至客戶端(動態(tài)輸出數(shù)據(jù)) * */ 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 發(fā)送數(shù)據(jù)線程 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("發(fā)送成功:"+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() { } }
網(wǎng)上有人說這個方式很LOW,而且不好,但現(xiàn)在我只會這個,就連asynctask也還在學(xué)習(xí)中。
還有一點,子線程更新主UI的方法:
我使用的是 Handler.post(); 同樣十分簡單的使用方法。
Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { textView.append("發(fā)送成功:"+text+"\n"); } });
關(guān)于幾種子線程更新主UI的方法,我以后會再寫一篇博客。我現(xiàn)在已經(jīng)知道了不下4中方法,還沒實地操作。
再來就是關(guān)于I/O流操作的方法:
這里我不做詳細介紹,大家自行Google!什么你不會Google? 自行找FQ工具吧~~
這里我說一下我對于I/O的簡單使用方法。
萬物始于最初的,InputStream,OutputStream,他的方法只有reader()和write()。再其之上的,例如什么使用最多的BufferedReader對象,都是再其之上的升級包裝,穿了個衣服,變得更加華麗而已。他的專業(yè)術(shù)語就是——裝飾者模式,感興趣的可以去翻翻資料。
最初,我對InputStream和OutputStream的方法不是很明了,經(jīng)常錯將InputStream用上write()的方法。 不知在哪里看到這么一段話,I/O流的操作實質(zhì)是相對于Socket,ServerSocket連接后產(chǎn)生的數(shù)據(jù)流管道,reader與write是讀取與寫入的意思,是相對于那個數(shù)據(jù)流管道進行操作的。即讀取管道里的信息,寫入信息至管道。不知道這樣大家動不動。
最后就是我們要學(xué)習(xí)的Socket。
他的使用也很簡單,建立一個Socket對象,設(shè)置IP和端口,獲取其I/O流。
ClientSocket = new Socket("10.0.2.2",9001); outputStream = ClientSocket.getOutputStream();
到這里基本的使用方法就這么多。
說說我在這其中遇到的問題:
1.模擬器連接Java服務(wù)器連接不上。IP設(shè)置不對,我最開始設(shè)置的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.忘記設(shè)置manifest,或者系統(tǒng)提供的android.permission.INTERNET全是大寫的??!說來滑稽,但我就是遇到這樣的問題,系統(tǒng)自動完成的代碼提示,全是大寫的,我以為就是那樣呢,結(jié)果明明應(yīng)該是小寫。
<uses-permission android:name="android.permission.INTERNET" />
忘記貼上java服務(wù)器的代碼了,補上。
/** * 本實例功能:接受服務(wù)器發(fā)來的數(shù)據(jù) */ 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(); } } } }
這是服務(wù)器端就收到的數(shù)據(jù):
OK,基本就這樣。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- Android使用socket創(chuàng)建簡單TCP連接的方法
- python服務(wù)器與android客戶端socket通信實例
- Android中Socket通信的實現(xiàn)方法概述
- Android NDK中socket的用法以及注意事項分析
- Android頂欄定時推送消息
- 使用SignalR推送服務(wù)在Android的實現(xiàn) SignalA
- Android中利用App實現(xiàn)消息推送機制的代碼
- Android Socket通信詳解
- Android編程之客戶端通過socket與服務(wù)器通信的方法
- Android 模擬器(JAVA)與C++ socket 通訊 分享
- Android中使用socket通信實現(xiàn)消息推送的方法詳解
相關(guān)文章
Qt5.12.6配置Android Arm開發(fā)環(huán)境(圖文)
本文主要介紹了Qt5.12.6配置Android Arm開發(fā)環(huán)境,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06android webview獲取html代碼和根據(jù)id獲取value實例
這篇文章主要介紹了android webview獲取html代碼和根據(jù)id獲取value實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android開發(fā)之ListView實現(xiàn)Item局部刷新
對于ListView數(shù)據(jù)的刷新大家都知道,改變Adapter的數(shù)據(jù)源,然后調(diào)用Adapter的notifyDateSetChanged()方法即可。通過本篇文章給大家詳細介紹Android開發(fā)之ListView實現(xiàn)Item局部刷新,感興趣的朋友一起學(xué)習(xí)吧2015-10-10