尝试将 GPS 坐标发送到 Android 模拟器

发布于 2024-11-29 07:03:43 字数 2880 浏览 1 评论 0原文

我正在尝试编写一个程序,使用 telnet 发送 GPS 坐标。
我不断收到以下异常:

Exception in thread "Timer-0" java.lang.NullPointerException
at org.apache.commons.net.telnet.Telnet._sendByte(Telnet.java:1060)
at org.apache.commons.net.telnet.TelnetOutputStream.write(TelnetOutputStream.java:87)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:77)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:111)
at java.io.PrintStream.write(PrintStream.java:430)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
at java.io.PrintStream.write(PrintStream.java:477)
at java.io.PrintStream.print(PrintStream.java:619)
at java.io.PrintStream.println(PrintStream.java:756)
at com.example.myandroid.gpsSender$1.run(gpsSender.java:34)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)

我不知道为什么会收到此异常。你能告诉我吗?谢谢 这是我的代码:

package com.example.myandroid;

import org.apache.commons.net.telnet.TelnetClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketException;
import java.util.Timer;
import java.util.TimerTask;

public class gpsSender {
    private TelnetClient telnet = new TelnetClient();

    public static void main(String[] args) throws Exception {
        gpsSender client = new gpsSender();
        client.start();
    }

    public String start() throws Exception {

        // Connect to the specified server

        telnet.connect("localhost", 5554);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            float longitude = 1;
            float latitude = 1;
            int count = 0;
            PrintStream out = new PrintStream(telnet.getOutputStream());

            public void run() {
                out.println("geo fix " + String.valueOf(longitude) + " "
                        + String.valueOf(latitude));
                out.flush();
                System.out.println("geo fix " + String.valueOf(longitude) + " "
                        + String.valueOf(latitude));
                longitude++;
                latitude++;
                count++;
                if (count > 1000) {
                    cancel();
                }
            }
        }, 0, 1000);
        try {
            telnet.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ("Done");
    }

    public void write(String value) {
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

I am trying to write a program that will send GPS coordinates using telnet.
I keep getting the following exception:

Exception in thread "Timer-0" java.lang.NullPointerException
at org.apache.commons.net.telnet.Telnet._sendByte(Telnet.java:1060)
at org.apache.commons.net.telnet.TelnetOutputStream.write(TelnetOutputStream.java:87)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:77)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:111)
at java.io.PrintStream.write(PrintStream.java:430)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
at java.io.PrintStream.write(PrintStream.java:477)
at java.io.PrintStream.print(PrintStream.java:619)
at java.io.PrintStream.println(PrintStream.java:756)
at com.example.myandroid.gpsSender$1.run(gpsSender.java:34)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)

I don't know why I am getting this. Can you please tell me? thanks
Here is my code:

package com.example.myandroid;

import org.apache.commons.net.telnet.TelnetClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketException;
import java.util.Timer;
import java.util.TimerTask;

public class gpsSender {
    private TelnetClient telnet = new TelnetClient();

    public static void main(String[] args) throws Exception {
        gpsSender client = new gpsSender();
        client.start();
    }

    public String start() throws Exception {

        // Connect to the specified server

        telnet.connect("localhost", 5554);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            float longitude = 1;
            float latitude = 1;
            int count = 0;
            PrintStream out = new PrintStream(telnet.getOutputStream());

            public void run() {
                out.println("geo fix " + String.valueOf(longitude) + " "
                        + String.valueOf(latitude));
                out.flush();
                System.out.println("geo fix " + String.valueOf(longitude) + " "
                        + String.valueOf(latitude));
                longitude++;
                latitude++;
                count++;
                if (count > 1000) {
                    cancel();
                }
            }
        }, 0, 1000);
        try {
            telnet.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ("Done");
    }

    public void write(String value) {
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

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

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

发布评论

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

评论(4

剑心龙吟 2024-12-06 07:03:43

该行将

 telnet.disconnect();

执行,因此您不会有可写入的输出流,因此出现 NPE。你应该删除该行。

The line

 telnet.disconnect();

is going to execute, so you won't have an output stream to write to, hence the NPE. You should remove that line.

抠脚大汉 2024-12-06 07:03:43

不要使用 localhost,而是尝试 10.0.2.2 - 这是模拟器通常所在的 IP 地址,但我不确定是否可以与模拟器建立 telnet 通信。

编辑:这是一个链接到模拟器的 telnet 但它来自命令窗口 - 也许您可以为您的测试编写一个小批处理脚本来发送 GPS 坐标,但如果您必须以某种方式将其与正在运行的测试同步,你会必须从 Android 应用程序执行此操作。看起来模拟器确实在本地主机上,而你的“电脑”在 10.0.2.2 上

Instead of using localhost, try 10.0.2.2 - that's the IP address the emulator is usually on but I'm not sure if you can establish telnet comms with the emulator.

Edit: Here's a link to telnet the emulator but it's from a command window - perhaps you could write a small batch script for your tests to send gps coordinates but if you have to syncronise this somehow with your running test, you would have to do it from the Android app. It looks like the emulator is indeed on localhost and your 'pc' is on 10.0.2.2

星星的轨迹 2024-12-06 07:03:43

当然,请使用 10.0.2.2 iP,并确保您在清单中授予了正确的权限..但如果您需要输入长字符,仍然如此。和纬度。要在另一个应用程序中使用它们..您实际上并不需要telnet,并且对于模拟器来说,有一个名为“模拟器控制”的小窗口,您可以在其中手动输入长字符。和纬度。但使用与模拟器的 telnet 通信,这可能不会发生。在真正的手机上,您可以使用“NetwProvider.getLocation()”,但当然您也不能在模拟器上测试它。

Of course Use the 10.0.2.2 iP and also make sure you're giving the right permissions in your manifest .. but still, if you need to input the long. and lat. to use them in another app .. you don't really need telnet and for the emulator there's a little window called "Emulator Control" where you input manually the long. and lat. but using telnet communication with the emulator, that probably doesn't happen. and on a real phone you can use the "NetwProvider.getLocation()" but of course you can't test that on an emulator as well..

深海夜未眠 2024-12-06 07:03:43

感谢您发布这个问题。这种方法让我可以在 Robotium 测试中轻松操作模拟器的硬件和传感器。以下是将模拟器的电池电量设置为 100% 的代码片段:

TelnetClient telnet = new TelnetClient();
telnet.connect("10.0.2.2",5554);
PrintStream out = new PrintStream(telnet.getOutputStream());
out.println("power capacity 100");
out.flush();    
telnet.disconnect();

Thanks for publishing this question. This approach lets me easily manipulate the hardware and the sensors of the emulator from within my Robotium tests. Here is a code snippet to set the battery charge of the emulator to 100%:

TelnetClient telnet = new TelnetClient();
telnet.connect("10.0.2.2",5554);
PrintStream out = new PrintStream(telnet.getOutputStream());
out.println("power capacity 100");
out.flush();    
telnet.disconnect();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文