如何在NanoHttpd中更改URL中的地址(http:// localhost:8080 / HELLO_WORLD)

时间:2015-12-16 07:14:05

标签: java android nanohttpd

我的查询是如何更改如何更改网址(http://localhost:8080/HELLO_WORLD)中的地址。我将HELLO_WORLD更改为欲望词。

 @Override
public Response serve(IHTTPSession session) {       
    String answer = "";
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(
                new InputStreamReader(appContext.getAssets().open("block.html")));
        // do reading, usually loop until end of file reading
        String mLine;
        while ((mLine = reader.readLine()) != null) {
            //process line
            answer += mLine;

        }
    } catch (IOException e) {
        //log the exception
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                //log the exception
                Log.d("BABAR", "EXception occured in serve()");
            }
        }
    }
    return newFixedLengthResponse(answer);
}

请建议我如何更改

1 个答案:

答案 0 :(得分:0)

我不知道这是不是你想要的,但你可以试试。 您必须按照以下步骤操作:

1-创建一个本地来存储您的服务器文件; 2 - 然后将实现NanoHttp服务器的类中的响应更改为:

@Override
public Response serve(IHTTPSession session) {
    String answer = "";
    try{
        FileReader filereader = new FileReader(contextoMain.local(localyourstorethefiles)+"/yourfolder/yourfile.html");
    BufferedReader reader = new BufferedReader(filereader);
    String line = "";
    while ((line = reader.readLine()) != null) {
        answer += line;
    }
    reader.close();

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

3 - 然后,在不放入8080 / yourfolder / yourfile的情况下调用localhost:8080