編輯:關於Android編程
android Socket實現簡單聊天小程序,供大家參考,具體內容如下
服務器端:
package org.hwq.echo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class TalkServer { public static void main(String[] args) throws IOException{ ServerSocket server = null; Socket client = null; BufferedReader in = null; PrintWriter out = null; try{ server = new ServerSocket(4700); client = server.accept(); out = new PrintWriter(client.getOutputStream()); in = new BufferedReader(new InputStreamReader(client.getInputStream())); String line = in.readLine(); while(!"bye".equals(line)){ System.out.println("client:"+line); out.println("echo:"+line); out.flush(); line = in.readLine(); } }catch (Exception e) { e.printStackTrace(); if(client !=null) client.close(); if(server != null) server.close(); } } }
手機端:
package org.hwq.cho; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class EchoActivity extends Activity implements OnClickListener { EditText show,msg; Button send; Handler handler; Socket client; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); handler = new MyHandler(); show = (EditText) findViewById(R.id.show); msg = (EditText) findViewById(R.id.msg); send = (Button) findViewById(R.id.send); send.setOnClickListener(this); } @Override public void onClick(View arg0) { String message = msg.getText().toString(); // System.out.println("msg:"+message); new EchoThread(EchoActivity.this,message).start(); } public class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { switch(msg.what){ case 1: Toast.makeText(EchoActivity.this, "建立連接失敗", 0).show(); break; case 2: String message = (String) msg.obj; System.out.println("Handler:"+message); show.append("\n"+message); break; } } } private class EchoThread extends Thread{ private Context context; private String msg; EchoThread(Context context,String msg){ this.context = context; this.msg = msg; } public void run(){ if(client == null){ try { client = new Socket("192.168.1.102",4700); } catch (IOException e) { Message message = new Message(); message.what = 1; handler.sendMessage(message); } } System.out.println("建立連接"); try{ BufferedReader in; BufferedReader input; PrintWriter out; in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream()); String line = msg; if(!"bye".equals(line)){ System.out.println("line:"+line); out.println(line); out.flush(); String echo = in.readLine(); System.out.println("server:"+echo); Message message = new Message(); message.obj = echo; message.what = 2; handler.sendMessage(message); } }catch (Exception e) { } } } }
注意幾點:
1、添加網絡權限
<uses-permission android:name="android.permission.INTERNET"/>
如果沒添加,無法使用socket連接網絡。
2、在新啟線程中不要使用android系統UI界面
在EchoThrad的run()方法裡面,有下面代碼:
if(client == null){ try { client = new Socket("192.168.1.102",4700); } catch (IOException e) { Message message = new Message(); message.what = 1; handler.sendMessage(message); } }
這裡的handler.sendMessage(message);是發送一個消息給handler,然後handler根據消息彈出一個Toast顯示連接失敗。如果這裡直接使用
Toast.makeText(EchoActivity.this, "建立連接失敗", 0).show();
會報如下錯:
Can't create handler inside thread that has not called Looper.prepare()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
public class MainFragmentPagerAdapter extends FragmentPagerAdapter implement
摘要:Json數據在安卓開發過程中是非常常見的,在請求服務器端數據的時候,服務器端返回的無非就是三種類型:HTML,XML,JSON。所以學習JSON對安卓程序員來說是非
今天終於有點時間,來寫了一下: 為RecyclerView實現下拉刷新和上拉加載更多。今天會在前面的兩篇文章的基礎上:RecyclerView系列之(1):為Recycl
今天拿到一個客戶新的訂單需求,大概有40多個需求,今天先講更改系統的藍牙/wifi 熱點/消息通知/sleep 添加 never選項,分別是:藍牙:系統界面顯示名字,重命