【发布时间】:2011-08-29 16:43:28
【问题描述】:
我在 android 中有一个应用程序,它是一种客户端-服务器。
我已经建立了这两者之间的连接,现在我正在通过套接字发送数据。
服务器发送数据,客户端从套接字读取数据。
服务器向套接字发送一些 GPS 数据(经度、纬度),为了真正发送它,我使用以下格式:
公共类坐标{
private final double lon;
private final double lat;
private final int workerId;
public Coordinate(double lat, double lon, int workerId) {
this.lat = lat;
this.lon = lon;
this.workerId=workerId;
}
public double getLon() {
return lon;
}
public double getLat() {
return lat;
}
public int getwId(){
return workerId;
}
}
因此,对于我要发送的每个经度和纬度,我都添加了一个 int 的 workerID。
所以最后我通过我的套接字发送了一个 Coordinate 类的实例。
Coordinate coord=new Coordinate(lat,lon,workerID);
os=new PrintStream(clientSocket.getOutputStream());
os.println(coord);
好吧,直到这里一切都很好。
在客户端,我以这种方式读取数据:
Scanner is=new Scanner(new DataInputStream(socket.getInputStream()));
while(is.hasNext())
{
try{
Coordinate coord=is.next();
System.out.println(coord.getLon());
}
catch(Exception e){
e.printStackTrace();
}
}
问题是我使用它从套接字输入流中读取的 Scanner 类返回 String,但我通过套接字发送的内容完全不同......
有人知道我如何将输入数据转换为坐标,或者我应该如何继续从我通过套接字发送的内容中找到纬度和经度?????
谢谢!
编辑:我使用这种格式是因为我需要这些数据!!
【问题讨论】: