Java Android中的UDP客户端线程
我试图在我的android应用程序中创建一个udp客户端线程,该线程跟踪设备(服务器发送其坐标),并使用google maps API在地图上显示它们。然而,我很困,因为我是Android开发的新手。我已经在android应用程序中测试了我的udp连接,它可以正常工作。我正在努力做一个新的线程。当我运行应用程序时,它会显示“不幸的是,地图已停止” 我在onCreate()中执行此操作 及 谢谢Java Android中的UDP客户端线程,java,android,multithreading,google-maps,udpclient,Java,Android,Multithreading,Google Maps,Udpclient,我试图在我的android应用程序中创建一个udp客户端线程,该线程跟踪设备(服务器发送其坐标),并使用google maps API在地图上显示它们。然而,我很困,因为我是Android开发的新手。我已经在android应用程序中测试了我的udp连接,它可以正常工作。我正在努力做一个新的线程。当我运行应用程序时,它会显示“不幸的是,地图已停止” 我在onCreate()中执行此操作 及 谢谢 private class Coordinates extends AsyncTask<Inte
private class Coordinates extends AsyncTask<Integer, Integer, Integer>
{
private LatLng ltnlng = null;
private boolean valid = false;
@Override
protected void onPreExecute()
{
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method
}
@Override
protected Integer doInBackground(Integer... port) {
try {
// int port = 5005 ;
byte[] buffer = new byte[2048] ;
final StringBuilder notification = new StringBuilder("IN TRY" ) ;
Toast.makeText(getApplicationContext(), notification, Toast.LENGTH_SHORT).show() ;
DatagramSocket dsocket = new DatagramSocket(null) ;
InetSocketAddress address = new InetSocketAddress("192.168.1.170", 5005) ;
dsocket.bind(address) ;
// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Now loop forever, waiting to receive packets and printing them.
while (true)
{
valid = false;
final StringBuilder notification1 = new StringBuilder("RECEIVING.." ) ;
Toast.makeText(getApplicationContext(), notification1, Toast.LENGTH_SHORT).show() ;
dsocket.receive(packet) ;
final StringBuilder notification2 = new StringBuilder("RECEIVED" ) ;
Toast.makeText(getApplicationContext(), notification2, Toast.LENGTH_SHORT).show() ;
String msg = new String(buffer, 0, packet.getLength()) ;
String delims = "[\\s]+" ;
String[] tokens = msg.split(delims) ;
if(tokens[1].equals("nan") || tokens[2].equals("nan") || tokens[1].equals("0.0") || tokens[2].equals("0.0") )
{
final StringBuilder notification3 = new StringBuilder("NO COORDINATES AVAILABLE" ) ;
Toast.makeText(getApplicationContext(), notification3, Toast.LENGTH_SHORT).show() ;
}
else
{
valid = true;
StringBuilder notification4 = new StringBuilder(tokens[1] + "," + tokens[2] ) ;
Toast.makeText(getApplicationContext(), notification4, Toast.LENGTH_SHORT).show() ;
ltnlng = new LatLng(Integer.parseInt(tokens[1]),Integer.parseInt(tokens[2]));
}
publishProgress() ;
packet.setLength(buffer.length) ;
}
}
catch (Exception e) {
final StringBuilder notification4 = new StringBuilder("ERROR:" + e.toString() ) ;
Toast.makeText(getApplicationContext(), notification4, Toast.LENGTH_SHORT).show() ;
}
return null;
}
protected void onProgressUpdate(Integer...params)
{
if(valid == true)
{
marker = mMap.addMarker(new MarkerOptions()
.title("PipeBomb")
.snippet("Tracking Device")
.position(ltnlng)
);
mMap.animateCamera(CameraUpdateFactory.newLatLng(ltnlng)) ;
final StringBuilder notification=new StringBuilder("New coords" ) ;
Toast.makeText(getApplicationContext(), notification, Toast.LENGTH_SHORT).show() ;
}
else
{
final StringBuilder notification3 = new StringBuilder("NO COORDINATES AVAILABLE" ) ;
Toast.makeText(getApplicationContext(), notification3, Toast.LENGTH_SHORT).show() ;
}
}
@Override
protected void onPostExecute(Integer params)
{
Toast.makeText(getApplicationContext(), "Maps has finished", Toast.LENGTH_LONG).show();
}
}
私有类任务
{
私有LatLng ltnlng=null;
private boolean valid=false;
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
//显示进度条对话框,然后调用doInBackground方法
}
@凌驾
受保护的整数doInBackground(整数…端口){
试一试{
//int端口=5005;
字节[]缓冲区=新字节[2048];
最终StringBuilder通知=新StringBuilder(“尝试中”);
Toast.makeText(getApplicationContext(),通知,Toast.LENGTH_SHORT.show();
DatagramSocket dsocket=新DatagramSocket(空);
InetSocketAddress地址=新的InetSocketAddress(“192.168.1.170”,5005);
dsocket.bind(地址);
//创建一个数据包以将数据接收到缓冲区中
DatagramPacket数据包=新的DatagramPacket(buffer,buffer.length);
//现在永远循环,等待接收数据包并打印它们。
while(true)
{
有效=错误;
最终StringBuilder通知1=新StringBuilder(“接收”);
Toast.makeText(getApplicationContext(),notification1,Toast.LENGTH_SHORT).show();
数据包接收(数据包);
最终StringBuilder通知2=新StringBuilder(“收到”);
Toast.makeText(getApplicationContext(),notification2,Toast.LENGTH_SHORT).show();
String msg=新字符串(缓冲区,0,packet.getLength());
字符串delims=“[\\s]+”;
String[]tokens=msg.split(delims);
如果(令牌[1]。等于(“nan”)|令牌[2]。等于(“nan”)|令牌[1]。等于(“0.0”)|令牌[2]。等于(“0.0”))
{
最终StringBuilder通知3=新StringBuilder(“无可用坐标”);
Toast.makeText(getApplicationContext(),notification3,Toast.LENGTH_SHORT).show();
}
其他的
{
有效=真;
StringBuilder notification4=新的StringBuilder(标记[1]+”,“+标记[2]);
Toast.makeText(getApplicationContext(),notification4,Toast.LENGTH_SHORT).show();
ltnlng=new-LatLng(Integer.parseInt(令牌[1]),Integer.parseInt(令牌[2]);
}
出版进度();
packet.setLength(buffer.length);
}
}
捕获(例外e){
最终StringBuilder通知4=新StringBuilder(“错误:+e.toString());
Toast.makeText(getApplicationContext(),notification4,Toast.LENGTH_SHORT).show();
}
返回null;
}
受保护的void onProgressUpdate(整数…参数)
{
如果(有效==true)
{
marker=mMap.addMarker(新MarkerOptions()
.标题(“管道炸弹”)
.snippet(“跟踪设备”)
.职位(ltnlng)
);
mMap.animateCamera(CameraUpdateFactory.newLatLng(ltnlng));
最终StringBuilder通知=新StringBuilder(“新坐标”);
Toast.makeText(getApplicationContext(),通知,Toast.LENGTH_SHORT.show();
}
其他的
{
最终StringBuilder通知3=新StringBuilder(“无可用坐标”);
Toast.makeText(getApplicationContext(),notification3,Toast.LENGTH_SHORT).show();
}
}
@凌驾
受保护的void onPostExecute(整数参数)
{
Toast.makeText(getApplicationContext(),“映射已完成”,Toast.LENGTH_LONG.show();
}
}
我编辑了这篇文章。但是,当我运行getCoordinates()时,一旦调用@ChiefTwoPencilsPlease并包含stracktrace,它就会崩溃。
private class Coordinates extends AsyncTask<Integer, Integer, Integer>
{
private LatLng ltnlng = null;
private boolean valid = false;
@Override
protected void onPreExecute()
{
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method
}
@Override
protected Integer doInBackground(Integer... port) {
try {
// int port = 5005 ;
byte[] buffer = new byte[2048] ;
final StringBuilder notification = new StringBuilder("IN TRY" ) ;
Toast.makeText(getApplicationContext(), notification, Toast.LENGTH_SHORT).show() ;
DatagramSocket dsocket = new DatagramSocket(null) ;
InetSocketAddress address = new InetSocketAddress("192.168.1.170", 5005) ;
dsocket.bind(address) ;
// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Now loop forever, waiting to receive packets and printing them.
while (true)
{
valid = false;
final StringBuilder notification1 = new StringBuilder("RECEIVING.." ) ;
Toast.makeText(getApplicationContext(), notification1, Toast.LENGTH_SHORT).show() ;
dsocket.receive(packet) ;
final StringBuilder notification2 = new StringBuilder("RECEIVED" ) ;
Toast.makeText(getApplicationContext(), notification2, Toast.LENGTH_SHORT).show() ;
String msg = new String(buffer, 0, packet.getLength()) ;
String delims = "[\\s]+" ;
String[] tokens = msg.split(delims) ;
if(tokens[1].equals("nan") || tokens[2].equals("nan") || tokens[1].equals("0.0") || tokens[2].equals("0.0") )
{
final StringBuilder notification3 = new StringBuilder("NO COORDINATES AVAILABLE" ) ;
Toast.makeText(getApplicationContext(), notification3, Toast.LENGTH_SHORT).show() ;
}
else
{
valid = true;
StringBuilder notification4 = new StringBuilder(tokens[1] + "," + tokens[2] ) ;
Toast.makeText(getApplicationContext(), notification4, Toast.LENGTH_SHORT).show() ;
ltnlng = new LatLng(Integer.parseInt(tokens[1]),Integer.parseInt(tokens[2]));
}
publishProgress() ;
packet.setLength(buffer.length) ;
}
}
catch (Exception e) {
final StringBuilder notification4 = new StringBuilder("ERROR:" + e.toString() ) ;
Toast.makeText(getApplicationContext(), notification4, Toast.LENGTH_SHORT).show() ;
}
return null;
}
protected void onProgressUpdate(Integer...params)
{
if(valid == true)
{
marker = mMap.addMarker(new MarkerOptions()
.title("PipeBomb")
.snippet("Tracking Device")
.position(ltnlng)
);
mMap.animateCamera(CameraUpdateFactory.newLatLng(ltnlng)) ;
final StringBuilder notification=new StringBuilder("New coords" ) ;
Toast.makeText(getApplicationContext(), notification, Toast.LENGTH_SHORT).show() ;
}
else
{
final StringBuilder notification3 = new StringBuilder("NO COORDINATES AVAILABLE" ) ;
Toast.makeText(getApplicationContext(), notification3, Toast.LENGTH_SHORT).show() ;
}
}
@Override
protected void onPostExecute(Integer params)
{
Toast.makeText(getApplicationContext(), "Maps has finished", Toast.LENGTH_LONG).show();
}
}