Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

161
LINES

< > BotCompany Repo | #1000435 // Android web server test using NanoHTTPD on port 8888

JavaX source code (Android) [tags: use-pretranspiled] - run with: the app

Libraryless. Click here for Pure Java version (2000L/16K/40K).

1  
!636
2  
3  
import android.widget.*;
4  
import android.view.*;
5  
import android.content.Context;
6  
import android.app.Activity;
7  
import java.util.*;
8  
import java.io.*;
9  
import java.net.*;
10  
11  
import java.nio.ByteBuffer;
12  
import java.nio.channels.FileChannel;
13  
import java.nio.charset.Charset;
14  
import java.security.KeyStore;
15  
import java.text.SimpleDateFormat;
16  
import java.util.logging.Level;
17  
import java.util.logging.Logger;
18  
import java.util.regex.Matcher;
19  
import java.util.regex.Pattern;
20  
import java.util.zip.GZIPOutputStream;
21  
22  
import javax.net.ssl.KeyManager;
23  
import javax.net.ssl.KeyManagerFactory;
24  
import javax.net.ssl.SSLContext;
25  
import javax.net.ssl.SSLServerSocket;
26  
import javax.net.ssl.SSLServerSocketFactory;
27  
import javax.net.ssl.TrustManagerFactory;
28  
29  
class MyHTTPD extends NanoHTTPD {
30  
31  
     /**
32  
     * Constructs an HTTP server on given port.
33  
     */
34  
    public MyHTTPD(int port) throws IOException {
35  
        super(port);
36  
    }
37  
38  
39  
@Override
40  
    public Response serve( String uri, Method method,
41  
            Map<String, String> header, Map<String, String> parms,
42  
            Map<String, String> files )
43  
    {
44  
        System.out.println( method + " '222" + uri + "' " );
45  
        String msg = "<html><body><h1>Hello server</h1>\n";
46  
        if ( parms.get("username") == null )
47  
            msg +=
48  
                "<form action='?' method='get'>\n" +
49  
                "  <p>Your name: <input type='text' name='username'></p>\n" +
50  
                "</form>\n";
51  
        else
52  
            msg += "<p>Hello, " + parms.get("username") + "!</p>";
53  
54  
        msg += "</body></html>\n";
55  
        return newFixedLengthResponse(msg);
56  
    }
57  
}
58  
59  
public class main {
60  
  private static MyHTTPD server;
61  
  private static int port = 8888;
62  
63  
  static class Lg {
64  
    Activity context;
65  
    ScrollView sv;
66  
    TextView tv;
67  
    StringBuilder buf = new StringBuilder();
68  
    
69  
    Lg(Activity context) {
70  
      this.context = context;
71  
      sv = new ScrollView(context);
72  
      tv = new TextView(context);
73  
      tv.setText(buf.toString());
74  
      sv.addView(tv);
75  
    }
76  
    
77  
    View getView() {
78  
      return sv;
79  
    }
80  
    
81  
    void print(final String s) {
82  
      context.runOnUiThread(new Runnable() {
83  
        public void run() {
84  
          buf.append(s);
85  
          tv.setText(buf.toString());
86  
        }
87  
      });
88  
    }
89  
    
90  
    void println(String s) {
91  
      print(s + "\n");
92  
    }
93  
  }
94  
  
95  
  static Lg lg;
96  
  
97  
  public static View main(final Activity context) {
98  
    lg = new Lg(context);
99  
    
100  
    OutputStream outputStream = new OutputStream() {
101  
      public void write(int b) {
102  
        try {
103  
          lg.print(new String(new byte[] {(byte) b}, "UTF-8")); // This is crap
104  
        } catch (UnsupportedEncodingException e) {}
105  
      }
106  
      
107  
      @Override
108  
      public void write(byte[] b, int off, int len) {
109  
        try {
110  
          lg.print(new String(b, off, len, "UTF-8")); // This is crap
111  
        } catch (UnsupportedEncodingException e) {}
112  
      }
113  
    };
114  
    
115  
    PrintStream ps = new PrintStream(outputStream, true);
116  
    System.setOut(ps);
117  
    System.setErr(ps);
118  
    
119  
    new Thread() {
120  
      public void run() {
121  
        try {
122  
          // actual main program!
123  
          
124  
          server = new MyHTTPD(port);
125  
          server.start();
126  
          System.out.println("HTTP server started (listening on port " + port + "!)");
127  
          printMyIPs();
128  
          
129  
        } catch (Throwable e) {
130  
          e.printStackTrace();
131  
        }
132  
      }
133  
    }.start();
134  
135  
    return lg.getView();
136  
  }
137  
  
138  
  static void printMyIPs() {
139  
  String ip;
140  
    try {
141  
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
142  
        while (interfaces.hasMoreElements()) {
143  
            NetworkInterface iface = interfaces.nextElement();
144  
            // filters out 127.0.0.1 and inactive interfaces
145  
            if (iface.isLoopback() || !iface.isUp())
146  
                continue;
147  
148  
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
149  
            while(addresses.hasMoreElements()) {
150  
                InetAddress addr = addresses.nextElement();
151  
                ip = addr.getHostAddress();
152  
                System.out.println(iface.getDisplayName() + " " + ip);
153  
            }
154  
        }
155  
    } catch (Throwable e) {
156  
        e.printStackTrace();
157  
    }
158  
  }
159  
}
160  
161  
!include #1000433 // class NanoHTTP

Author comment

Began life as a copy of #1000432

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, exkalrxbqyxc, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000435
Snippet name: Android web server test using NanoHTTPD on port 8888
Eternal ID of this version: #1000435/1
Text MD5: 7923b98f71e0548b79fb461609d212b0
Transpilation MD5: 0f9c6c48cc203fefe10f2cec16f32ac5
Author: stefan
Category: javax android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-06 00:39:19
Source code size: 4419 bytes / 161 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1068 / 808
Referenced in: [show references]