MrX
MrX

Reputation: 993

Show Toast on method Response NanoHTTPD

I use NanoHTTPD (this) to make simple server. I follow this tutorial too for testing. I search deep inside the issue on its GitHub but I haven't found any solution. I only want is to show a simple Toast when someone open the address given from the my project in their browser.

Here is the code for my 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;
}
}

I have one singleton class with static Context so I can guarantee the context from code above is not null.

The problem is, when I comment line Toast like above , everything is work like normal. The browser from user show the output from Response method. But when I used Toast, it's like the Android not send the data into browser, so the browser not show the properly output.

Edit

Here is my log:

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$3.<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$1.handle(NanoHTTPD.java:376) 
 at NanoHTTPD.NanoHTTPD$1.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$3.<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$1.handle(NanoHTTPD.java:376)
 at NanoHTTPD.NanoHTTPD$1.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)

Upvotes: 0

Views: 1027

Answers (3)

MrX
MrX

Reputation: 993

As @pskink comment above. I have done with this problem. Googling with this keyword android toast background thread and I found the solution. Below the code :

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

Upvotes: 0

greenapps
greenapps

Reputation: 11214

You cannot show a Toast() in the serve() of NanoHttpd as that is executed in a thread.

Indeed you will not see an exception. They are already catched by nano.

Upvotes: 1

Omkar
Omkar

Reputation: 3100

IMO problem with your Context context try getApplicationContext() instead context like

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

OR

so create Constructor in Server class

public Server (Context ctx){

this.context = ctx;

}

and call this constructor to initialize Context object in your Activity Class like

Server server = new Server (MainActivity.this);

Upvotes: 0

Related Questions