前段时间出差去解决一个问题,就是获取接入点,还有登录模式。手机网络差分设置的问题,在这过程是使用Ntrip协议的。这个有点坑的地方,是在于服务器那边是不是按照协议来标准处理这些验证数据。首先是一个socket连接,socket连接上了发送协议。这个协议就是Ntrip来做的。
Ntrip协议的下载地址:点击
package com.aualarm.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class ClientInfoThread extends Thread{
Socket socket = null;
Handler handler;
String host = "10.10.100.254"; // 默认:tcp server 端口 20108
int port = 8899; //默认端口888
boolean need_send_cmd = false;
DataInputStream dis = null;
DataOutputStream dos = null;
public ClientInfoThread(Handler handler) {
this.handler = handler;
}
public void set_host_port(String host,int port) {
this.host = host;
this.port = port;
}
public void run() {
try {
byte readBuffer[] = new byte[64];
try {
//socket = new Socket(host,port);
socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(host,port);
socket.connect(socketAddress,2000);
socket.setSoTimeout(2000);
socket.setTcpNoDelay(true);
dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
PublicInfo.connected_server = true;
Message msg = new Message();
msg.what = 0x1236;
msg.obj = readBuffer;
handler.sendMessage(msg);
while (!Thread.interrupted()) {
if (PublicInfo.need_send_cmd) {
PublicInfo.need_send_cmd = false;
Log.i("Show", PublicInfo.buf.toString());
dos.write(PublicInfo.buf, 0, PublicInfo.buf_length);
dos.flush();
}
int count = 0;
try {
count = dis.read(readBuffer);
} catch (IOException e) {
continue;
}
if (count < 1)
continue;
String line = null;
line = new String(readBuffer, 0, count);
Message msg2 = new Message();
msg2.what = 0x1234;
msg2.obj = line;
msg2.arg1 = count;
handler.sendMessage(msg2);
}
}finally {
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (socket != null)
socket.close();
PublicInfo.info_thread_exit = true;
PublicInfo.connected_server = false;
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = 0x1235;
msg.obj = e.getMessage();
handler.sendMessage(msg);
}
}
}
发送数据的数据结构类
public class PublicInfo {
public static byte buf[] = null;
public static int buf_length;
public static boolean need_send_cmd;
public static boolean recv_thread_exit;
public static boolean info_thread_exit;
public static boolean connected_server;
}
这个是socket连接启动的,一般都要开一个线程来连接的。
if (PublicInfo.connected_server) { // 已经连接服务器的,需要断开后重新连接
disconnect_server();
}
PublicInfo.connected_server = false;
client_info_thread = new ClientInfoThread(netMessageHandle);
client_info_thread.set_host_port("10.10.100.254",
SharePreferenceUtil.DEVICE_PORT);
client_info_thread.start();
线程接收到数据,在界面上处理的消息机制。
// 消息 handler 对象
Handler netMessageHandle = new Handler() {
public void handleMessage(Message msg) {
// 如果消息是 0x1234,则是从 线程中 传输过来的数据
if (msg.what == 0x1234) {
show_result(msg.obj.toString(), msg.arg1); // 将 缓冲区的数据显示到 UI
}
// 如果消息是 0x1234,则是从 线程中 传输过来的数据
if (msg.what == 0x1235) {
Toast.makeText(getApplicationContext(),
getString(R.string.MainNetClose) + msg.obj,
Toast.LENGTH_LONG).show();
}
if (msg.what == 0x1236) {
}
}
};
GET /HTTP/1.1
User-Agent: NTRIP NETSET_DEBUG
Accept: */*
Connection: close
Authorization: Basic MDE5MzowNzEw
String strResult[] = strDataString.split("STR;");
for (int i = 1; i < strResult.length; i++) {
StreamDetails temDetails = new StreamDetails();
// 第二次按";"标志,截取字符串
String strFinallResult[] = strResult[i].split(";");
temDetails.strMountpoint = strFinallResult[0];
add(temDetails);
}
GET /RTCM31 HTTP/1.1
User-Agent: NTRIP 54168078020418
Accept: */*
Connection: close
Authorization: Basic dGVzdDoyOTc4MzE=
//用户密码转化为Base64编码
String userPassword = "test" + ":" + "297831";
String base64UserPassword = "";
try {
base64UserPassword = URLDecoder.decode(Base64.encodeToString(userPassword.getBytes(), Base64.NO_WRAP), "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}