本文主要是介绍android 使用Ntrip协议,socket连接获取接入点,登录模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前段时间出差去解决一个问题,就是获取接入点,还有登录模式。手机网络差分设置的问题,在这过程是使用Ntrip协议的。这个有点坑的地方,是在于服务器那边是不是按照协议来标准处理这些验证数据。首先是一个socket连接,socket连接上了发送协议。这个协议就是Ntrip来做的。
Ntrip协议的下载地址:点击
1、使用背景
使用最多就是手机卡的接入点设置,android 系统设置,移动网络,可以新建apn设置。有些专卡,专网使用需要自已设置apn。平时我们自已使用的手机网络的接入点都是默认的。如下图:
2、代码socket连接
需要参数就是ip地址,端口就行了。socket连接类,以及发送数据,接收数据等等。
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 端口 20108int 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) { } } };
3、Ntrip协议
a、连接成功后的,获取接入点协议
GET /HTTP/1.1
User-Agent: NTRIP NETSET_DEBUG
Accept: */*
Connection: close
Authorization: Basic MDE5MzowNzEw
NETSET_DEBUG这个是调试的,这里可以携带参数可以自定义
Authorization: Basic MDE5MzowNzEw 这一行用户是验证,有些服务器是要带参数,就算携带的参数user:psw组成,通过base64编码的。
获取接入点时,有些服务器不带这个参数不给通过,这个用户名和密码是错的也没多大关系,但是要有携带。不携带是这样的Authorization: Basic
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);
}
b、登录模式
首先是获取接入点,成功之后会给你一堆数据,自已进行解析,然后解析到接入点,放到头部携带GET /RTCM31 HTTP/1.1,其中/RTCM31就是接入点GET /RTCM31 HTTP/1.1
User-Agent: NTRIP 54168078020418
Accept: */*
Connection: close
Authorization: Basic dGVzdDoyOTc4MzE=
dGVzdDoyOTc4MzE=是basic64编码,可以看本文下载地址的文档。参数user:psw组成,通过base64编码的。
类似下面代码的写法:
//用户密码转化为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();
}
ICY 200 OK
还有其它返回可以看此下载地址的文档:Ntrip协议的下载地址: 点击
这篇关于android 使用Ntrip协议,socket连接获取接入点,登录模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!