NanoHTTPD - Cannot access Response class

1.5k views Asked by At

I'm following this post on SO because I want to handle POST requests with NanoHTTPD. When I put it into my MainActivity class in Android Studio, it gives me an error:

'Response(fi.iki.elonen.NanoHTTPD.Response.lStatus,java.lang.String,java.io.lnputStream, long)’ has protected access in 'fi.iki.elonen.NanoHTTPD.Response'

There's no quick fix suggested by Android Studio, unfortunately, so how would I fix this?

My code:

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.
}
1

There are 1 answers

0
Ngọc Tuấn On

You can use: newFixedLengthResponse to replace new Response, like: newFixedLengthResponse("Hello").