【问题标题】:NanoHTTPD - Cannot access Response classNanoHTTPD - 无法访问响应类
【发布时间】:2016-12-18 02:34:21
【问题描述】:

我在 SO 上关注 this post,因为我想使用 NanoHTTPD 处理 POST 请求。当我将它放入 Android Studio 中的 MainActivity 类时,它给了我一个错误:

'Response(fi.iki.elonen.NanoHTTPD.Response.lStatus,java.lang.String,java.io.lnputStream, long)' 在 'fi.iki.elonen.NanoHTTPD.Response' 中具有受保护的访问权限

很遗憾,Android Studio 没有建议快速修复,那么我该如何解决呢?

我的代码:

public Response serve(IHTTPSession session) {
    Map<String, String> files = new HashMap<String, String>();
    Method method = session.getMethod();
    if (Method.PUT.equals(method) || Method.POST.equals(method)) {
        try {
            session.parseBody(files);
        } 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());
        }
    }
    // get the POST body
    String postBody = session.getQueryParameterString();
    // or you can access the POST request's parameters
    String postParameter = session.getParms().get("parameter");

    return new Response(postBody); // Or postParameter.
}

【问题讨论】:

  • 发布演示问题的最小代码。我们不会挖掘您的链接。
  • @greenapps 已更改
  • 编译时错误。哪个声明导致了它?为什么让我们猜?
  • @greenapps 每次提到“响应”类时,都会突出显示每个“新响应”。

标签: java android nanohttpd


【解决方案1】:

您可以使用:newFixedLengthResponse 来替换新的 Response,例如: newFixedLengthResponse("你好")。

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2018-01-03
    • 2017-10-09
    • 1970-01-01
    相关资源
    最近更新 更多