简体   繁体   English

试图将 GPS 坐标发送到 android 仿真器

[英]Trying to send GPS coordinates to the android emulator

I am trying to write a program that will send GPS coordinates using telnet.我正在尝试编写一个程序,该程序将使用 telnet 发送 GPS 坐标。
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();
        }

    }

}

The line线

 telnet.disconnect();

is going to execute, so you won't have an output stream to write to, hence the NPE.将要执行,所以你不会有 output stream 要写入,因此是 NPE。 You should remove that line.您应该删除该行。

Thanks for publishing this question.感谢您发布此问题。 This approach lets me easily manipulate the hardware and the sensors of the emulator from within my Robotium tests.这种方法让我可以在我的 Robotium 测试中轻松地操作模拟器的硬件和传感器。 Here is a code snippet to set the battery charge of the emulator to 100%:以下是将模拟器的电池电量设置为 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();

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.不要使用 localhost,而是尝试 10.0.2.2 - 那是模拟器通常打开的 IP 地址,但我不确定您是否可以使用模拟器建立 telnet 通信。

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.编辑:这是一个远程登录模拟器的链接,但它来自命令 window - 也许你可以为你的测试编写一个小批处理脚本来发送 gps 坐标但是如果你必须以某种方式与你的运行测试同步,你必须这样做来自 Android 应用程序。 It looks like the emulator is indeed on localhost and your 'pc' is on 10.0.2.2看起来模拟器确实在本地主机上,而您的“电脑”在 10.0.2.2 上

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.当然使用 10.0.2.2 iP 并确保您在清单中提供正确的权限..但是,如果您需要输入长。 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.在另一个应用程序中使用它们..你真的不需要telnet,对于模拟器,有一个叫做“模拟器控制”的小window,你可以手动输入长。 and lat.和纬度。 but using telnet communication with the emulator, that probably doesn't happen.但是使用与模拟器的 telnet 通信,这可能不会发生。 and on a real phone you can use the "NetwProvider.getLocation()" but of course you can't test that on an emulator as well..在真正的手机上,你可以使用“NetwProvider.getLocation()”,但当然你也不能在模拟器上测试它..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM