public static void main(String[] args) throws IOException {
    WebServer webServer = new WebServer("localhost", 8080);
    webServer.registerHandler(
        new RequestHandler() {

          @Override
          public String handle(String query) {

            // We get the current date by creating a date object without any arguments whatsoever.
            Date currentDate = new Date();

            // We wrap the string to integer conversion into a so called exception block.
            // If something goes wrong a default value is set.
            int upperRandomLimit;
            try {
              upperRandomLimit = Integer.valueOf(query);
            } catch (NumberFormatException e) {
              upperRandomLimit = 10;
            }
            int randomNumber = new Random().nextInt(upperRandomLimit);

            return "Current date: "
                + currentDate
                + ". Random number in [0, "
                + upperRandomLimit
                + "): "
                + randomNumber;
          }
        });

    webServer.startServer();
  }
예제 #2
0
  @Override
  public void onCreate() {
    Toast.makeText(this, "Creating WebServerService", Toast.LENGTH_SHORT).show();
    Log.i("HTTPSERVICE", "Creating and starting httpService");
    super.onCreate();
    server = new WebServer(this);
    server.startServer();

    Toast.makeText(this, "WebServerService.startServer...", Toast.LENGTH_SHORT).show();
  }