向服务器发送 GPS 数据不起作用?

发布于 2024-11-28 00:27:39 字数 2537 浏览 1 评论 0原文

我正在尝试将 GPS 数据从 Android 手机发送到服务器。但它不起作用。我在这里附上了我的代码片段。请检查并帮助我解决这个问题!

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 文件已创建!但是当我输入“cat”查看里面的内容时,文件是空的!

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 格式)。我震惊了!请帮我解决这个问题!

I'm trying to send GPS data from android phone to server. Its not working though. I've attached my code snippet here. Kindly check it and help me with this!

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);          
    }

My output is like this! Its getting encoded and sent.

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

data sent to server is,

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

Data is sent to server. the new1.nmea file is created! but when I put 'cat' to see whats inside, the file is empty!

server.php

<?php 
//$data = $_POST["DATA"].""; 
$data = file_get_contents('php://input');
$Handle = fopen("/xxx/xxx/new1.nmea", "a");
fwrite($Handle, $data);
fclose($Handle);
?>

I need the data in the same format at the server side without any changes(nmea 0183 format). I'm struck! please help me with this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与之呼应 2024-12-05 00:27:40

为什么同时需要 NMEA 和 LocationUpdates,两者都可以。
当您说需要 GPS 信息时,您需要 NMEA 字符串还是仅需要精度、时间、速度等位置属性?

当您尝试发送 NMEA 字符串时,您应该在 NMEAListener 中执行 HTTP 请求,而不是在 LocationListener 中执行?我很好奇,这首先是什么逻辑。

Why do you need both NMEA and LocationUpdates, either of them will do.
When you say you want GPS information do you want NMEA string or just the location attributes like accuracy, time, speed, etc?

You should perform your HTTP request in NMEAListener as you are trying to send NMEA string and not in LocationListener? What was the logic for this in first place, am curious.

神爱温柔 2024-12-05 00:27:40

它的工作人员!

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);
?>

its working guys!

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);
?>
哆兒滾 2024-12-05 00:27:40

如果必须使用 HTTP 来接受数据,则应考虑使用“POST”而不是“GET”。 NMEA 中有很多字符,需要对其进行正确的 url 编码才能使请求通过。如果执行“POST”,您可以指定“内容类型”和“字符集”以匹配原始 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 字符串。

If you have to use HTTP to accept your data, you should consider using a "POST" instead of a "GET". NMEA has a lot of characters in it that would be required to be url encoded properly for the request to come through. If you do a "POST" you can specify the "Content Type" and "Charset" to match raw NMEA. I believe that you could just do "Content-Type: text/plain" and pass along the raw POST.

Note that if you went with the raw "POST" method, you would no longer have you $_POST variable in PHP and you would have to read the raw message. You can read a raw post by doing the following:

$data = file_get_contents('php://input');

Client side code:

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);

Note that your POST will come in the PHP script as a raw post, so you will have to do a little processing to just get the NMEA string out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文