在Android中使用NanoHTTPD [英] Using NanoHTTPD in Android

查看:807
本文介绍了在Android中使用NanoHTTPD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NanoHTTP来提供一个HTML文件。然而,NanoHTTP相对未记录在案,而我是新来的机器人。我的问题是,我在哪里保存的HTML文件,以及如何具体可以用NanoHTTP我为它服务了。

I am trying to use NanoHTTP to serve up an HTML file. However, NanoHTTP is relatively un-documented, and I am new to Android. My question is, where do I store the html file, and how specifically can I serve it up using NanoHTTP.

推荐答案

一个迟到的答案,但可能对别人有用。

A late answer but may be useful to others.

下面是一个简单的Hello Web服务器,而不是你问什么,但可以继续从这里开始。下面的程序假设你有一个 WWW 目录中的SD卡的根目录和文件 index.html的里面。

Here is a simple hello Web Server, not exactly what you ask, but you can continue from here. The following program supposes you have a www directory in the root of the SD Card and a file index.html inside.

主要活动 Httpd.java

package com.inforscience.web;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.*;
import java.util.*;


public class Httpd extends Activity
{
    private WebServer server;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        server = new WebServer();
        try {
            server.start();
        } catch(IOException ioe) {
            Log.w("Httpd", "The server could not start.");
        }
        Log.w("Httpd", "Web server initialized.");
    }


    // DON'T FORGET to stop the server
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (server != null)
            server.stop();
    }

    private class WebServer extends NanoHTTPD {

        public WebServer()
        {
            super(8080);
        }

        @Override
        public Response serve(String uri, Method method, 
                              Map<String, String> header,
                              Map<String, String> parameters,
                              Map<String, String> files) {
            String answer = "";
            try {
                // Open file from SD Card
                File root = Environment.getExternalStorageDirectory();
                FileReader index = new FileReader(root.getAbsolutePath() +
                        "/www/index.html");
                BufferedReader reader = new BufferedReader(index);
                String line = "";
                while ((line = reader.readLine()) != null) {
                    answer += line;
                }
                reader.close();

            } catch(IOException ioe) {
                Log.w("Httpd", ioe.toString());
            }


            return new NanoHTTPD.Response(answer);
        }
    }

}

显然, NanoHTTPD 类必须在同一个包。

Obviously the NanoHTTPD class must be in the same package.

您需要在的Andr​​oidManifest.xml 授予网络权限。

You need to grant internet permission in AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />

编辑::要访问服务器打开你的网页浏览器与您的设备,如的IP 192.168.1.20:8080

To access the server open you web browser with the IP of your device, e.g. 192.168.1.20:8080.

注:

  • 在经过​​测试,在安卓2.3
  • 在使用80端口限制root用户(<一个href="http://www.mail-archive.com/android-developers@googlegroups.com/msg47377.html">http://www.mail-archive.com/android-developers@googlegroups.com/msg47377.html).

这篇关于在Android中使用NanoHTTPD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆