我正在尝试从Android手机向服务器发送GPS数据。虽然不行。我在这里附上了我的代码片段。请检查并帮助我!
public void onNmeaReceived(long timestamp, String nmea)
{
String url = "http://www.xyz.com/server.php?DATA=";
String params = URLEncoder.encode(nmea);
url = url+params;
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
HttpResponse httpResponse = client.execute(httppost);
Log.d("Status", "Request Sent " + httpResponse.getStatusLine().getStatusCode());
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("url", url);
}
我的输出是这样的!它被编码并发送。
08-03 22:37:01.062: DEBUG/url(15874): http://www.xyz.com/server.php?DATA=%24GPGSV%2C4%2C1%2C16%2C03%2C14%2C147%2C%2C06%2C05%2C140%2C%2C09%2C05%2C018%2C%2C11%2C73%2C251%2C*7E%0D%0A
08-03 22:37:01.172: DEBUG/url(15874): http://www.xyz.com/server.php?DATA=%24GPGSV%2C4%2C2%2C16%2C14%2C29%2C085%2C%2C17%2C%2C%2C%2C18%2C%2C%2C%2C19%2C48%2C147%2C*72%0D%0A
08-03 22:37:01.312: DEBUG/url(15874): http://www.xyz.com/server.php?DATA=%24GPGSV%2C4%2C3%2C16%2C20%2C14%2C213%2C%2C22%2C29%2C056%2C%2C24%2C57%2C260%2C%2C27%2C07%2C001%2C*75%0D%0A
08-03 22:37:01.432: DEBUG/url(15874): http://www.xyz.com/server.php?DATA=%24GPGSV%2C4%2C4%2C16%2C28%2C32%2C298%2C%2C32%2C36%2C194%2C%2C08%2C%2C%2C%2C31%2C%2C%2C*74%0D%0A
08-03 22:37:01.582: DEBUG/url(15874): http://www.xyz.com/server.php?DATA=%24GPGGA%2C%2C%2C%2C%2C%2C0%2C%2C%2C%2C%2C%2C%2C%2C*66%0D%0A
08-03 22:37:01.702: DEBUG/url(15874): http://www.xyz.com/server.php?DATA=%24GPVTG%2C%2CT%2C%2CM%2C%2CN%2C%2CK%2CN*2C%0D%0A
08-03 22:37:01.848: DEBUG/url(15874): http://www.xyz.com/server.php?DATA=%24GPRMC%2C%2CV%2C%2C%2C%2C%2C%2C%2C%2C%2C%2CN*53%0D%0A
08-03 22:37:01.962: DEBUG/url(15874): http://www.xyz.com/server.php?DATA=%24GPGSA%2CA%2C1%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C*1E%0D%0A
发送到服务器的数据是,
http://www.xyz.com/server.php?DATA=%24GPGSA%2CA%2C1%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C*1E%0D%0A
将数据发送到服务器。 new1.nmea文件已创建!但当我把'猫'看到里面的东西时,文件是空的!
server.php
<?php
//$data = $_POST["DATA"]."";
$data = file_get_contents('php://input');
$Handle = fopen("/xxx/xxx/new1.nmea", "a");
fwrite($Handle, $data);
fclose($Handle);
?>
我需要在服务器端使用相同格式的数据而不做任何更改(nmea 0183格式)。我很震惊!请帮帮我!
答案 0 :(得分:1)
为什么你需要NMEA和LocationUpdates,他们中的任何一个都可以。 当你说你想要GPS信息时你想要NMEA字符串还是只需要准确度,时间,速度等位置属性?
当您尝试发送NMEA字符串而不是在LocationListener中时,您应该在NMEAListener中执行HTTP请求吗?首先,这是什么逻辑,我很好奇。
答案 1 :(得分:0)
如果必须使用HTTP接受数据,则应考虑使用“POST”而不是“GET”。 NMEA中有很多字符需要正确编码以满足请求的要求。如果您执行“POST”,则可以指定“内容类型”和“Charset”以匹配原始NMEA。我相信你可以做“Content-Type:text / plain”并传递原始POST。
请注意,如果您使用原始的“POST”方法,那么您将不再拥有PHP中的$ _POST变量,并且您必须阅读原始消息。您可以通过执行以下操作来阅读原始帖子:
$data = file_get_contents('php://input');
客户端代码:
String url = "http://www.xyz.com/server.php";
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(nmea);
se.setContentType("text/plain");
post.setEntity(se);
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(post);
请注意,您的POST将作为原始帖子出现在PHP脚本中,因此您必须进行一些处理以获取NMEA字符串。
答案 2 :(得分:0)
其工作人员!
public void onNmeaReceived(long timestamp, String nmea)
{
String params;
try {
params = URLEncoder.encode(nmea, "UTF-8");
Log.d("executing", params);
String url = "http://www.xyz.com/server.php?DATA="+params;
//url = url+params;
HttpPost httppost = new HttpPost(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse httpResponse = client.execute(httppost);
Log.d("Status", "Request Sent " + httpResponse.getStatusLine().getStatusCode());
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
<?php
$data = $_GET["DATA"]."";
echo urldecode($data);
//$data = file_get_contents('php://input');
$Handle = fopen("xxx/xxx/test.nmea", "a");
fwrite($Handle, $data);
fclose($Handle);
flush($Handle);
?>