Socket实现Android客户端与服务器的通信
生活随笔
收集整理的這篇文章主要介紹了
Socket实现Android客户端与服务器的通信
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言:題目要求故寫一下總結(jié)。
1.什么是Socket?
網(wǎng)絡(luò)用語叫套接字原理是基于tcp/ip 協(xié)議的一種通信手段,目前題目中要求無非就是當(dāng)客戶端數(shù)據(jù)異常時推送給服務(wù)器報警信息
往常接下來都是先看效果圖的,由于今天回宿舍有點早,準(zhǔn)備有點匆忙,所以演示沒準(zhǔn)備,但代碼都是測試通過的,直接貼代碼吧.
* 2.MainActivity主類*
public class MainActivity extends Activity {EditText ip;EditText editText;TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ip = (EditText) findViewById(R.id.ip);editText = (EditText) findViewById(R.id.editText);tv = (TextView) findViewById(R.id.tv);//連接按鈕findViewById(R.id.connect).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {connect();//連接}});//發(fā)送數(shù)據(jù)按鈕findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {send();}});}// ------------------------------------------------------------------------------------Socket socket = null;BufferedWriter writer = null;BufferedReader reader = null;/*** 異步 實現(xiàn)socket與服務(wù)器的連接*/public void connect() {AsyncTask<Void, String, Void> read = new AsyncTask<Void, String, Void>() {@Overrideprotected Void doInBackground(Void... arg0) {try {socket = new Socket(ip.getText().toString(), 40000);writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));publishProgress("@success");} catch (UnknownHostException e1) {System.out.println(e1.toString());} catch (IOException e1) {System.out.println(e1.toString());}try {String line;while ((line = reader.readLine()) != null) {publishProgress(line);}reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}@Overrideprotected void onProgressUpdate(String... values) {if (values[0].equals("@success")) {Toast.makeText(MainActivity.this, "連接成功",Toast.LENGTH_SHORT).show();}tv.append("別人說:" + values[0] + "\n");super.onProgressUpdate(values);}};read.execute();}/*** socket 發(fā)送方法*/private void send() {try {tv.append("我說" + editText.getText().toString() + "\n");writer.write(editText.getText().toString() + "\n");writer.flush();editText.setText("");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }3.xml布局
<LinearLayout xmlns:tools="http://schemas.android.com/tools"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"tools:context=".MainActivity" ><EditText android:id="@+id/ip"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="10.67.66.195" ></EditText><Button android:id="@+id/connect"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="連接" /><TextView android:id="@+id/tv"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="0.85" /><EditText android:id="@+id/editText"android:hint="發(fā)送的消息"android:layout_width="match_parent"android:layout_height="wrap_content" /><Button android:id="@+id/send"android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="send"android:text="發(fā)送" /></LinearLayout>4.總結(jié)
代碼挺簡單的,因為題目需求不高,所以就一個異步加載的方法實現(xiàn)與服務(wù)器的通信即可。
其實如果覺得麻煩,甚至還可以繼續(xù)簡化一些,直接把socket寫在一個子線程就可以了
new Thread(){public void run() {try {Socket socket = new Socket("192.168.100.1",4000);.......} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}};}.start();這種方法,甚至上上面異步都會牽扯如下問題:
無法程序啟動后自動連接服務(wù)器、服務(wù)器未開啟監(jiān)聽前程序啟動會崩等等
具體這兩種方式出現(xiàn)的如上問題都已解決,以及通過socket實現(xiàn)android客戶端接收服務(wù)器端的圖片等,后面慢慢總結(jié)…
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Socket实现Android客户端与服务器的通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XML的注释踩坑记录
- 下一篇: /usr/lib/gcc/x86_64-