I stuck for this problem for a day. I follow this tutorial for testing. I search deep inside on the issues but I dont find 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 code 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;
}
}`
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 , its like the Android not send the data into browser, so the browser not show the properly output. Any advice will help for me. Thanks in advance
Edit :
Here 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)
To show Toast use this code
new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show(); } });
I stuck for this problem for a day. I follow this tutorial for testing. I search deep inside on the issues but I dont find 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 code my Server.class :
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 , its like the Android not send the data into browser, so the browser not show the properly output. Any advice will help for me. Thanks in advance
Edit : Here 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)