# AndroidSocket
**Repository Path**: wpmufeng/AndroidSocket
## Basic Information
- **Project Name**: AndroidSocket
- **Description**: A simple Android socket communication, you're gonna love it
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2021-11-24
- **Last Updated**: 2021-11-24
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# AndroidSocket #
A simple Android socket communication, you're gonna love it!

Add AndroidSocket to your project
-----
#### Gradle
compile 'com.virtue.androidsocket:AndroidSocket:1.0.7'
#### Maven
startService(new Intent(this, MySocketService.class));
#### In the service, configure the relevant parameters.(服务中配置连接参数)
Socketer.getInstance(getApplicationContext()).bindServerConnect("123.57.56.201", 20083) //配置socket地址和端口
.setTimeout(15).setEncode("UTF_8") //Configure Timeout and encoding,Timeout unit is seconds配置超时时间与编码
.setReceiveType(ReceiveType.SEPARATION_SIGN) //Configuring the Receive Type配置接收形式以分隔符接收
.setEndCharSequence("\r\n") //"\r\n" is End for split 配置结束符
.setSendMaxByteLength(1500).start(); //Send Max bytes配置一次性最多发送的消息字节数
或者or:
Socketer.getInstance(getApplicationContext()).bindServerConnect("123.57.56.201", 20083)
.setTimeout(15).setEncode("UTF_8")
.setReceiveType(ReceiveType.FIXED_LENGTH) //Configuring the Receive Type配置接收形式以分隔符接收
.setMsgLength(2048) //Fixed length receive 配置固定长度大小接收
.setSendMaxByteLength(1500).start(); //配置一次性最多发送的消息字节数
Case1. Auto Parse ! (自动解析包含服务器主推通知和请求响应两种数据)
-----
#### If the service has unsolicited information to you, you need to register a broadcast, like this:(如果服务有主推通知消息,你需要注册以下广播)
//Set parse to Auto
Socketer.getInstance(MainActivity.this).setParseMode(ParseMode.AUTO_PARSE);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BroadCastType.SERVER_NOTICE);
MessageReceiver dataReceiver = new MessageReceiver();
registerReceiver(dataReceiver, intentFilter);
#### Broadcast reception is as follows:(广播接收如下)
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BroadCastType.SERVER_NOTICE)) {
String dataStr = intent.getStringExtra(BroadCastType.SERVER_NOTICE_DATA);
Log.i(TAG, "Data given to me by the server:" + dataStr);
}
}
#### Send a request to the server(发送请求到服务器)
Socketer.getInstance(MainActivity.this).sendStrData(reDataStr, "\"seq\":100", new ResponseListener() {
@Override
public void onSuccess(final String data) {
Log.i("Test server data", "callback data:" + data);
runOnUiThread(new Runnable() {
@Override
public void run() {
tvResponse.setText(data);
}
});
}
@Override
public void onFail(int failCode) {
Log.e("Test server data", "callback error:" + failCode);
}
});
其中参数1代表是请求的数据,参数2代表是返回数据中的唯一标识,可以是请求ID、token值或者能标识唯一性的字符串
Where parameter 1 represents the requested data, parameter 2 represents a unique identity in the returned data, either a request ID, a token value, or a string that uniquely identifies the uniqueness.
For bugs, feature requests, and discussion please use GitHub Issues