-2

I want to make a http server in android which serve file (html/png..) also doing event based on request.

Example: If request is /maketoast android make a toast or request is /chanhetext android change a specific textview text.

Point is: I already make a server by Nanohttpd. It's serve files, but it don't make any event, like make toast or change textview text.

Here is Nanohttpd serve method

   public Response serve(IHTTPSession session) {
       String msg = "<html><body><h1>Hello server</h1>\n";
       Map<String, String> parms = session.getParms();
       if (parms.get("username") == null) {
           msg += "<form action='?' method='get'>\n  <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
       } else {
           msg += "<p>Hello, " + parms.get("username") + "!</p><img src=max.png/>";
       }
       //Toast.makeText(mContext, "Helloooooo...!!!", Toast.LENGTH_SHORT).show();
       return newFixedLengthResponse( msg + "</body></html>\n" );
    }

When I try to make toast server going freeze and doesn't give any response. Can any one please tell me how I make this kind of thing.

Huy Nguyen
  • 2,025
  • 3
  • 25
  • 37
Emran
  • 188
  • 1
  • 12

2 Answers2

0

Try:

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
           Toast.makeText(mContext, "Helloooooo...!!!", Toast.LENGTH_SHORT).show();
        }
    });
kgandroid
  • 5,507
  • 5
  • 39
  • 69
0

Put your Nanohttpd into a service, so that you can send a LocalBroadCast to your activity to display Toast or any other UI response.

RRTW
  • 3,160
  • 1
  • 35
  • 54