Search code examples
androidnanohttpd

android change textview text based on http request


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.


Solution

  • Try:

    getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
               Toast.makeText(mContext, "Helloooooo...!!!", Toast.LENGTH_SHORT).show();
            }
        });