yeelink平台账号
在yeelink平台上创建一个设备并创建一个GPS类型的传感器
使用Java开发一个HTTP客户端发送GPS数据到Yeelink的GPS传感器上客户端代码如下:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.Socket;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Date;public class SimpleHttpClientForGPS { private static String encoding = "GBK"; public static void main(String[] args) { try { Socket s = new Socket("42.96.164.52", 80); OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream()); StringBuffer sb = new StringBuffer(); String data = "{\"timestamp\":\"2015-07-26T16:13:14\",\"value\":{\"lat\":22.61667,\"lng\":114.066,\"speed\":84}}"; System.out.println(data); sb.append("POST /v1.0/device/164918/sensor/200614/datapoint HTTP/1.1\r\n"); sb.append("Host:api.yeelink.net\r\n"); // sb.append("Content-Type:application/json\r\n"); sb.append("Connection:Keep-Alive\r\n"); sb.append("Content-Type:application/json\r\n"); sb.append("Content-Length:"+data.length()+"\r\n"); sb.append("Connection:Keep-Alive\r\n"); sb.append("U-ApiKey:7b9368da6014b53783da391284efe4b6\r\n"); //注,这是关键的关键,忘了这里让我搞了半个小时。这里一定要一个回车换行,表示消息头完,不然服务器会等待 sb.append("\r\n"); System.out.println("len="+data.length()); osw.write(sb.append(data).toString()); // osw.write(data); osw.flush(); //--输出服务器传回的消息的头信息 InputStream is = s.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String tmp=""; while((tmp=br.readLine())!=null) { System.out.println(tmp); } br.close(); //关闭流 is.close(); System.out.println(sb); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static String readLine(InputStream is, int contentLe) throws IOException { ArrayList lineByteList = new ArrayList(); byte readByte; int total = 0; if (contentLe != 0) { do { readByte = (byte) is.read(); lineByteList.add(Byte.valueOf(readByte)); total++; } while (total < contentLe);//消息体读还未读完 } else { do { readByte = (byte) is.read(); lineByteList.add(Byte.valueOf(readByte)); } while (readByte != 10); } byte[] tmpByteArr = new byte[lineByteList.size()]; for (int i = 0; i < lineByteList.size(); i++) { tmpByteArr[i] = ((Byte) lineByteList.get(i)).byteValue(); } lineByteList.clear(); return new String(tmpByteArr, encoding); } }
运行客户端代码,yeelink服务端返回0,表示发送成功{"timestamp":"2015-07-26T16:13:14","value":{"lat":22.61667,"lng":114.066,"speed":84}}len=85HTTP/1.1 200 OKServer: nginx/1.1.19Date: Sun, 26 Jul 2015 09:30:27 GMTContent-Type: text/html; charset=UTF-8Transfer-Encoding: chunkedConnection: closeX-Powered-By: PHP/5.3.10-1ubuntu3.6Set-Cookie: CAKEPHP=3ddenne0i0m92ov2l6p8oqfdu7; expires=Mon, 03-Aug-2015 17:30:27 GMT; path=/P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"0POST /v1.0/device/164918/sensor/200614/datapoint HTTP/1.1Host:api.yeelink.netConnection:Keep-AliveContent-Type:application/jsonContent-Length:85Connection:Keep-AliveU-ApiKey:7b9368da6014b53783da391284efe4b6{"timestamp":"2015-07-26T16:13:14","value":{"lat":22.61667,"lng":114.066,"speed":84}}
GPS的数据必须要包含经度,纬度以及速度,缺一不可,如:{"timestamp":"2015-07-26T16:13:14","value":{"lat":22.61667,"lng":114.066,"speed":84}}