android studio中nanohttpd的工作示例

时间:2015-09-29 01:03:45

标签: android webserver nanohttpd

我一直试图让一个简单的nanohttpd服务器运行,但我无法弄清楚如何设置它。 我试图按照本指南操作: Using NanoHTTPD in Android 尝试了第一个答案,但我在这一行得到一个错误: “私人WebServer服务器;” “无法解析符号'WebServer'”

也试过这样: https://github.com/NanoHttpd/nanohttpd/wiki/How-to-use-NanoHttpd 这一行的另一个错误: “return newFixedLengthResponse(sb.toString());” “错误'newFixedLengthResponse无法解决。”

我尝试添加nanohttpd.class以及导入nanohttpd.jar但似乎没有什么区别。

有没有人知道nanohttpd的一个简单的'hello world'指南实际上有效吗? 或者可能是一个更加文档化的简单web服务器用于android?

1 个答案:

答案 0 :(得分:0)

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

我在我的应用程序的子类中实例化ClockServer类。 如您所见,serve方法返回结果如下:

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

这里是ClockServer类:

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 {
    }
}