2011-05-16 52 views
0

我想發送這個經緯度和長期的GPS當前位置在我的Android和我的Python服務器之間的開放套接字。發送經緯度和長期使用開放套接字

public void onLocationChanged(Location loc) 
{ 
    GeoPoint point = new GeoPoint((int) (loc.getLatitude() * 1E6), 
       (int) (loc.getLongitude() * 1E6));  
    String Text = "My current location is: " + 
      "Latitud =" + loc.getLatitude()+ 
      "Longitud =" +loc.getLongitude() ; 
    Toast.makeText(getApplicationContext(), 
      Text, 
      Toast.LENGTH_SHORT).show(); 
} 

,所以我將它保存在字符串文本和我添加按鈕,將其連接到服務器,現在我想lat和郎每次發送所以如何使用開放套接字來發送這個字符串?

回答

1
String cmd("my command"); 
    try { 
     InetSocketAddress address = new InetSocketAddress("10.1.1.1", 12350); 
     DatagramPacket request = new DatagramPacket(cmd.getBytes(), cmd.length(), address); 
     DatagramSocket socket = new DatagramSocket(); 
     socket.send(request); 
    } catch (SocketException e) { 

     ... 
     } 
    } catch (IOException e) { 

     ... 
     } 
    }