android studio 中 nanohttpd 的工作示例

working example of nanohttpd in android studio

我一直在尝试获得一个简单的 nanohttpd 服务器 运行 但我不知道如何设置它.. 我试图遵循本指南: 尝试了第一个答案,但我在这一行收到错误: "private WebServer server;""cannot resolve symbol 'WebServer'"

还尝试了以下操作: https://github.com/NanoHttpd/nanohttpd/wiki/How-to-use-NanoHttpd 此行的另一个错误: "return newFixedLengthResponse(sb.toString());" "error 'newFixedLengthResponse cannot be resolved."

我尝试只添加 nanohttpd.class 以及导入 nanohttpd.jar,但似乎都没有什么不同。

有谁知道一个简单的 'hello world' nanohttpd 实际有效指南? 或者 android?

的文档更多的简单 Web 服务器

这是我使用 NanoHTTPD 编写的一个相当简单的应用程序的代码。 它被称为 'ClockServer' 因为我在数字时钟应用程序中使用它,允许远程设置时钟的数字颜色、背景颜色和亮度。

我在我的应用程序的应用程序子class中实例化了时钟服务器class。 如您所见,serve 方法 returns 结果如下:

return new NanoHTTPD.Response(Response.Status.OK, MIME_JSON, responseString);

这是时钟服务器 class:

import android.content.res.Resources;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import fi.iki.elonen.NanoHTTPD;


public class ClockServer extends NanoHTTPD {

    private static final String MIME_JSON = "application/json";
    private Clock clock;

    public ClockServer(Clock clock, int port) {
        super(port);
        this.clock = clock;
    }

    @Override
    public Response serve(IHTTPSession session) {
        try {
            Method method = session.getMethod();
            String uri = session.getUri();
            Map<String, String> parms = session.getParms();
            String responseString = serveClock(session, uri, method, parms);
            return new NanoHTTPD.Response(Response.Status.OK, MIME_JSON, responseString);

        } catch (IOException ioe) {
            return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
        } catch (ResponseException re) {
            return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
        } catch (NotFoundException nfe) {
            return new NanoHTTPD.Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found");
        } catch (Exception ex) {
            return new Response(Response.Status.INTERNAL_ERROR, MIME_HTML, "<html><body><h1>Error</h1>" + ex.toString() + "</body></html>");
        }
    }

    private String serveClock(IHTTPSession session, String uri, Method method, Map<String, String> parms)  throws IOException, ResponseException {
        String responseString = "";
        do {
            if(Method.GET.equals(method)) {
                responseString = handleGet(session, parms);
                break;
            }

            if(Method.POST.equals(method)) {
                responseString = handlePost(session);
                break;
            }

            throw new Resources.NotFoundException();

        } while(false);

        return responseString;
    }

    private String handleGet(IHTTPSession session, Map<String, String> parms) {
        return clock.handleRequest("{'name':'status', 'value':''}");
    }

    private String handlePost(IHTTPSession session) throws IOException, ResponseException {
        Map<String, String> files = new HashMap<String, String>();
        session.parseBody(files);

        return clock.handleRequest(files.get("postData"));
    }


    private class NotFoundException extends RuntimeException {
    }
}