try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), “utf-8”));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), “utf-8”));
while(isStartServer) {
if(reader.ready()) {
String data = reader.readLine();
JSONObject json = new JSONObject(data);
SocketMessage msg = new SocketMessage();
msg.to = json.getInt(“to”);
msg.msg = json.getString(“msg”);
msg.from = socketID;
msg.time = getTime(System.currentTimeMillis());
mMsgList.add(msg);
System.out.println(“收到一条消息:”+json.getString(“msg”)+" >>>> to socketID:"+json.getInt(“to”));
}
Thread.sleep(100);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
-
得到此刻的时间
-
@param millTime
-
@return
*/
private String getTime(long millTime) {
Date d = new Date(millTime);
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
System.out.println(sdf.format(d));
return sdf.format(d);
}
public static void main(String[] args) {
MyServer server = new MyServer();
server.startSocket();
}
}
(1)先来看看manifest:
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.jw.socketclient”
android:versionCode=“1”
android:versionName=“1.0” >
<application
android:allowBackup=“true”
android:icon=“@drawable/ic_launcher”
android:label=“@string/app_name”
android:theme=“@style/AppTheme” >
<activity
android:name=“com.jw.socketclient.MainActivity”
android:label=“@string/app_name” >
(2)用到的依赖:
implementation ‘androidx.legacy:legacy-support-v4:1.0.0’
implementation ‘androidx.recyclerview:recyclerview:1.1.0’
(3)界面:
activity_main.xml
<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”
tools:context=“.MainActivity”
android:orientation=“vertical” >
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”>
<Button
android:id=“@+id/start_btn”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:text=“start”/>
<Button
android:id=“@+id/stop_btn”
android:text=“stop”
android:layout_weight=“1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<EditText
android:id=“@+id/socket_id_edt”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:hint=“socketID”/>
<androidx.recyclerview.widget.RecyclerView
android:id=“@+id/rv”
android:layout_width=“match_parent”
android:layout_height=“0dp”
android:layout_weight=“1”/>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“50dp”
android:background=“#E3ECE3”
<EditText
android:id=“@+id/msg_edt”
android:layout_marginLeft=“30dp”
android:layout_marginRight=“10dp”
android:layout_width=“160dp”
android:layout_height=“40dp”
android:background=“#ffffff”
android:layout_gravity=“center”
android:layout_weight=“9”
/>
<Button
android:id=“@+id/send_btn”
android:layout_width=“40dp”
android:layout_height=“35dp”
android:text=“发送”
android:textColor=“#ffffff”
android:layout_gravity=“center”
android:background=“#0B9C10”
android:layout_weight=“1”/>
item.xml(右边的界面,展示自己发出的消息)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:orientation=“vertical”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:padding=“6dp”
<LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:gravity=“center_horizontal”
android:orientation=“vertical” >
<TextView
android:id=“@+id/tv_time”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:background=“#ECE8E8”
android:padding=“2dp”
android:textColor=“#ffffff”
android:textSize=“12sp” />
<RelativeLayout
android:layout_marginTop=“5dp”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
<ImageView
android:id=“@+id/iv_userhead”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“true”
android:layout_alignParentRight=“true”
android:background=“@drawable/head_img”
android:focusable=“false” />
<TextView
android:id=“@+id/tv_name”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toRightOf=“@id/iv_userhead”
android:textColor=“#818181”
android:textSize=“15sp” />
<TextView
android:id=“@+id/tv”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginRight=“10dp”
android:layout_toLeftOf=“@id/iv_userhead”
android:clickable=“true”
android:focusable=“true”
android:lineSpacingExtra=“2dp”
android:minHeight=“50dp”
android:gravity=“center”
android:background=“@drawable/chat_to”
android:textColor=“#ff000000”
android:textSize=“15sp” />
item2.xml(左边界面,展示别人发过来的消息)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”
android:padding=“6dp”
<LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:gravity=“center”
android:orientation=“vertical” >
<TextView
android:id=“@+id/tv_time2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:background=“#ECE8E8”
android:padding=“2dp”
android:textColor=“#ffffff”
android:textSize=“12sp” />
<RelativeLayout
android:layout_marginTop=“5dp”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
<ImageView
android:id=“@+id/iv_userhead2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“true”
android:layout_alignParentLeft=“true”
android:background=“@drawable/feedback”
android:focusable=“false” />
<TextView
android:id=“@+id/tv_name2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_toLeftOf=“@id/iv_userhead2”
android:textColor=“#818181”
android:textSize=“15sp”
android:layout_alignParentLeft=“true”
/>
<TextView
android:id=“@+id/tv2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginLeft=“10dp”
android:layout_toRightOf=“@id/iv_userhead2”
android:clickable=“true”
android:focusable=“true”
android:gravity=“center”
android:lineSpacingExtra=“2dp”
android:minHeight=“50dp”
android:background=“@drawable/chat_from”
android:textColor=“#ff000000”
android:textSize=“15sp”
/>
MainActivity.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.app.Activity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class MainActivity extends Activity implements OnClickListener {
private EditText mSocketIDEdt, mMessageEdt;
private StringBuffer mConsoleStr = new StringBuffer();
private Socket mSocket;
private boolean isStartRecieveMsg;
private RecyclerView rv;
private SocketHandler mHandler;
protected BufferedReader mReader;
protected BufferedWriter mWriter;
private static final int COMPLETED = 1;
private ArrayList list;
private MyAdapter adapter;
Thread thread;
private static boolean flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mSocketIDEdt = (EditText) findViewById(R.id.socket_id_edt);
mMessageEdt = (EditText) findViewById(R.id.msg_edt);
findViewById(R.id.start_btn).setOnClickListener(this);
findViewById(R.id.send_btn).setOnClickListener(this);
findViewById(R.id.stop_btn).setOnClickListener(this);
mHandler = new SocketHandler();
rv = (RecyclerView) findViewById(R.id.rv);
list = new ArrayList<>();
adapter = new MyAdapter(this);
rv.setAdapter(adapter);
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(manager);
}
private void initSocket() {
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
isStartRecieveMsg = true;
mSocket = new Socket(“192.168.2.172”, 2000);
mReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream(), “utf-8”));
mWriter = new BufferedWriter(new OutputStreamWriter(mSocket.getOutputStream(), “utf-8”));
while(isStartRecieveMsg) {
if(mReader.ready()) {
mHandler.obtainMessage(0, mReader.readLine()).sendToTarget();
}
Thread.sleep(200);
}
mWriter.close();
mReader.close();
mSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_btn:
send();
break;
case R.id.start_btn:
if(!isStartRecieveMsg) {
initSocket();
}
break;
case R.id.stop_btn:
flag=true;
System.err.println(“中断”);
default:
break;
}
}
private void send() {
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String… params) {
sendMsg();
return null;
}
}.execute();
}
protected void sendMsg() {
try {
String socketID = mSocketIDEdt.getText().toString().trim();
String msg = mMessageEdt.getText().toString().trim();
JSONObject json = new JSONObject();
json.put(“to”, socketID);
json.put(“msg”, msg);
mWriter.write(json.toString()+“\n”);
mWriter.flush();
mConsoleStr.append(“我” +msg+" “+getTime(System.currentTimeMillis())+”\n");
MyBean bean = new MyBean(“我”,2,msg,getTime(System.currentTimeMillis()));
list.add(bean);
/**
- 在子线程更新UI
*/
Message message = new Message();
message.what = COMPLETED;
mHandler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
class SocketHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 0:
try {
JSONObject json = new JSONObject((String)msg.obj);
MyBean bean = new MyBean(json.getString(“from”),1,json.getString(“msg”),getTime(System.currentTimeMillis()));
list.add(bean);
} catch (Exception e) {
e.printStackTrace();
}
// 向适配器set数据
adapter.setData(list);
rv.setAdapter(adapter);
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(manager);
rv.scrollToPosition(adapter.getItemCount()-1);//recycleview滑到底部
break;
case 1:
// 向适配器set数据
adapter.setData(list);
rv.setAdapter(adapter);
manager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(manager);
rv.scrollToPosition(adapter.getItemCount()-1);//recycleview滑到底部
break;
default:
break;
}
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
isStartRecieveMsg = false;
}
private String getTime(long millTime) {
Date d = new Date(millTime);
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
System.out.println(sdf.format(d));
return sdf.format(d);
}
}
MyAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter {
private Context context;
private ArrayList data;
private static final int TYPEONE = 1;
private static final int TYPETWO = 2;
public MyAdapter(Context context) {
this.context = context;
}
public void setData(ArrayList data) {
this.data = data;
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return data.get(position).getNumber();
}
@Override
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/7556fc5a7cc3fc11bb69fe48adadea75.jpeg)
学习分享,共勉
Android高级架构师进阶之路
题外话,我在阿里工作多年,深知技术改革和创新的方向,Android开发以其美观、快速、高效、开放等优势迅速俘获人心,但很多Android兴趣爱好者所需的进阶学习资料确实不太系统,完整。今天我把我搜集和整理的这份学习资料分享给有需要的人
- Android进阶知识体系学习脑图
- Android进阶高级工程师学习全套手册
- 对标Android阿里P7,年薪50w+学习视频
- 大厂内部Android高频面试题,以及面试经历
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-ctk8V6bi-1712400937629)]
[外链图片转存中…(img-1vvaZ7AS-1712400937630)]
[外链图片转存中…(img-k2vMgymO-1712400937630)]
[外链图片转存中…(img-558KdDK2-1712400937630)]
[外链图片转存中…(img-sX2XDaKS-1712400937631)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/7556fc5a7cc3fc11bb69fe48adadea75.jpeg)
学习分享,共勉
Android高级架构师进阶之路
题外话,我在阿里工作多年,深知技术改革和创新的方向,Android开发以其美观、快速、高效、开放等优势迅速俘获人心,但很多Android兴趣爱好者所需的进阶学习资料确实不太系统,完整。今天我把我搜集和整理的这份学习资料分享给有需要的人
- Android进阶知识体系学习脑图
[外链图片转存中…(img-bOZcPlcm-1712400937631)]
- Android进阶高级工程师学习全套手册
[外链图片转存中…(img-vexgK64u-1712400937631)]
- 对标Android阿里P7,年薪50w+学习视频
[外链图片转存中…(img-xVWynmuF-1712400937632)]
- 大厂内部Android高频面试题,以及面试经历
[外链图片转存中…(img-uCONbxSW-1712400937632)]