About Android calling nanohttpd class to access html in LAN

Keywords: Android Session Web Server network

Want to record the problems encountered in the work, just learned to call the nanohttpd class, concise and clear. Attach the download address of nanohttpd package https://github.com/NanoHttpd/nanohttpd

First of all, this paper introduces the use of nanohttpd here. You can build a lightweight Web server through this class. To realize the function, you need to connect to the same local area network. When the PC accesses the address where Android device connects to the local area network, it can open the html file in the directory. Dry goods:

1. Start the thread first,

 1 public class MainActivity extends Activity {
 2     private SimpleServer server;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6             super.onCreate(savedInstanceState);
 7             server = new SimpleServer();
 8             try {
 9                 server.start();
10                 Log.i("Httpd",Server startup);
11             } catch(IOException ioe) {
12                 Log.w("Httpd",server startup failed);
13             }
14     }

2. How can I do without a port number for the Web server? The default port number I set here is 8080

    public SimpleServer() {
        super(8080);
    }

3. At this point, enter the data processing function. If you want to open a file, you must first index it to the directory of the file

    @Override
    public Response serve(IHTTPSession session) {
        LogTools.d(TAG, "OnRequest:" + session.getUri());
        String uri = session.getUri();//Index file name
        String pathname = path + uri;
        LogTools.d(TAG, path + uri);
            return FileStream(session,pathname);
    }
    public Response FileStream(IHTTPSession session, String pathname) {
        try {
            FileInputStream fis = new FileInputStream(pathname);
            LogTools.d(TAG, pathname);
            return Response.newChunkedResponse(Status.OK,readHtml(pathname),fis);
        } catch (FileNotFoundException e){
            e.printStackTrace();
            return response404(session,pathname);
        }
    }

 

 

 

4. Turn html file into text mode by index file name

    private String readHtml(String pathname) {
        BufferedReader br=null;
        StringBuffer sb = new  StringBuffer();
        try {
            br=new BufferedReader(new InputStreamReader(new FileInputStream(pathname),  "UTF-8"));
            String temp=null;
            while((temp=br.readLine())!=null){
                sb.append(temp);
            }
        } catch (FileNotFoundException e) {
            LogTools.e(TAG, "Missing operating system!");
            e.printStackTrace();
        } catch (IOException e) {
            LogTools.e(TAG, "write error!");
            e.printStackTrace();
        }
        LogTools.d(TAG, sb.toString());
        return sb.toString();
    }

5. Send to the server in the form of stream

        public Response(IStatus status, String msg, InputStream data, int i) {
            this(Status.OK, MIME_HTML, msg);
        }

6. Close thread

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (server != null){
            server.stop();
        }
        Log.w("Httpd", "The server stopped.");
    }

  

For example, if I input 192.168.5.101:8080/index.html in IE, I can open / mnt/sdcard/webView/index.html in the Android directory. I hope it can help you.

Posted by sfullman on Mon, 06 Jan 2020 19:47:05 -0800