!636 import android.widget.*; import android.view.*; import android.content.Context; import android.app.Activity; import java.util.*; import java.io.*; import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.security.KeyStore; import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPOutputStream; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLServerSocket; import javax.net.ssl.SSLServerSocketFactory; import javax.net.ssl.TrustManagerFactory; class MyHTTPD extends NanoHTTPD { /** * Constructs an HTTP server on given port. */ public MyHTTPD(int port) throws IOException { super(port); } @Override public Response serve( String uri, Method method, Map header, Map parms, Map files ) { System.out.println( method + " '222" + uri + "' " ); String msg = "

Hello server

\n"; if ( parms.get("username") == null ) msg += "
\n" + "

Your name:

\n" + "
\n"; else msg += "

Hello, " + parms.get("username") + "!

"; msg += "\n"; return newFixedLengthResponse(msg); } } public class main { private static MyHTTPD server; private static int port = 8888; static class Lg { Activity context; ScrollView sv; TextView tv; StringBuilder buf = new StringBuilder(); Lg(Activity context) { this.context = context; sv = new ScrollView(context); tv = new TextView(context); tv.setText(buf.toString()); sv.addView(tv); } View getView() { return sv; } void print(final String s) { context.runOnUiThread(new Runnable() { public void run() { buf.append(s); tv.setText(buf.toString()); } }); } void println(String s) { print(s + "\n"); } } static Lg lg; public static View main(final Activity context) { lg = new Lg(context); OutputStream outputStream = new OutputStream() { public void write(int b) { try { lg.print(new String(new byte[] {(byte) b}, "UTF-8")); // This is crap } catch (UnsupportedEncodingException e) {} } @Override public void write(byte[] b, int off, int len) { try { lg.print(new String(b, off, len, "UTF-8")); // This is crap } catch (UnsupportedEncodingException e) {} } }; PrintStream ps = new PrintStream(outputStream, true); System.setOut(ps); System.setErr(ps); new Thread() { public void run() { try { // actual main program! server = new MyHTTPD(port); server.start(); System.out.println("HTTP server started (listening on port " + port + "!)"); printMyIPs(); } catch (Throwable e) { e.printStackTrace(); } } }.start(); return lg.getView(); } static void printMyIPs() { String ip; try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); System.out.println(iface.getDisplayName() + " " + ip); } } } catch (Throwable e) { e.printStackTrace(); } } } !include #1000433 // class NanoHTTP