在方法 Response NanoHTTPD 上显示 Toast

Show Toast on method Response NanoHTTPD

我也使用 NanoHTTPDthis) to make simple server. I follow this 教程进行测试。我在其 GitHub 上深入搜索问题,但我还没有找到任何解决方案。我只想当有人在浏览器中打开我的项目中给出的地址时,显示一个简单的 Toast

这是我的 Server.class 的代码:

public class Server extends NanoHTTPD {

private static Server server = null;
Context context;

@Override
public Response serve(IHTTPSession session) {
    String msg = "My Server in Android\n";
//        context = ProgramProperties.getAppContext();

    if (session.getMethod() == Method.GET) {
        Map<String,String> headers = session.getHeaders();
        if (headers.get("username") != null) {
            String username = headers.get("username");
            msg += "Hi, " + username;   /* Output for browser */
//                Toast.makeText(context, "Receive Connection, Hello " + username , Toast.LENGTH_SHORT).show();
        } else {
            msg+="Wrong headers parameters";
//                Toast.makeText(context, "Receive Connection with null header", Toast.LENGTH_SHORT).show();
        }
    }
    return newFixedLengthResponse(msg + "</body></html>");
}

private Server() throws IOException {
    super(8080);

}

public static Server getServer() throws IOException{
    if(server == null){

        server = new Server();
    }
    return server;
}
}

我有一个带有静态上下文的单例 class,所以我可以保证上面代码中的 context 不为空。

问题是,当我像上面那样注释行 Toast 时,一切正常。用户的浏览器显示 Response 方法的输出。但是当我使用 Toast 时,就像 Android 没有将数据发送到浏览器,所以浏览器无法正确显示输出。

编辑

这是我的日志:

02-14 12:31:54.574 25609-26777/com.test.vhp.testproject E/NanoHTTPD: Communication with the client broken, or an bug in the handler code java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
 at android.os.Handler.<init>(Handler.java:200) 
 at android.os.Handler.<init>(Handler.java:114) 
 at android.widget.Toast$TN.<init>(Toast.java:350)
 at android.widget.Toast$TN.<init>(Toast.java:350) 
 at android.widget.Toast.<init>(Toast.java:107) 
 at android.widget.Toast.makeText(Toast.java:263) 
 at Server.serve(Server.java:34) at NanoHTTPD.NanoHTTPD.handle(NanoHTTPD.java:376) 
 at NanoHTTPD.NanoHTTPD.handle(NanoHTTPD.java:372) 
 at NanoHTTPD.NanoHTTPD.handle(NanoHTTPD.java:535) 
 at NanoHTTPD.HTTPSession.execute(HTTPSession.java:421) 
 at NanoHTTPD.ClientHandler.run(ClientHandler.java:75) 
 at java.lang.Thread.run(Thread.java:761) 
02-14 12:32:04.882 25609-26932/com.test.vhp.testproject E/NanoHTTPD: Communication with the client broken, or an bug in the handler code java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
 at android.os.Handler.<init>(Handler.java:200) 
 at android.os.Handler.<init>(Handler.java:114) 
 at android.widget.Toast$TN.<init>(Toast.java:350) 
 at android.widget.Toast$TN.<init>(Toast.java:350) 
 at android.widget.Toast.<init>(Toast.java:107) 
 at android.widget.Toast.makeText(Toast.java:263) at Server.serve(Server.java:34)
 at NanoHTTPD.NanoHTTPD.handle(NanoHTTPD.java:376)
 at NanoHTTPD.NanoHTTPD.handle(NanoHTTPD.java:372) 
 at NanoHTTPD.NanoHTTPD.handle(NanoHTTPD.java:535) 
 at NanoHTTPD.HTTPSession.execute(HTTPSession.java:421)
 at NanoHTTPD.ClientHandler.run(ClientHandler.java:75) 
 at java.lang.Thread.run(Thread.java:761)

你的 Context contextIMO 问题 尝试 getApplicationContext() 而不是 context 就像

Toast.makeText(getApplicationContext(), "Receive Connection, Hello " + username , Toast.LENGTH_SHORT).show();

因此在 Server class

中创建 Constructor
public Server (Context ctx){

this.context = ctx;

}

并调用此构造函数来初始化您 Activity Class 中的 Context 对象,例如

Server server = new Server (MainActivity.this);

您不能在 NanoHttpd 的 serve() 中显示 Toast(),因为它是在线程中执行的。

确实你不会看到异常。他们已经被nano抓住了。

正如上面的@pskink 评论。我已经解决了这个问题。使用此关键字 android toast background thread 进行谷歌搜索,我找到了解决方案。代码下方:

handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, "Receive Connection, Hello " + username , Toast.LENGTH_SHORT).show();
                }
            });