如何在 NanoHttpd 中更改 url (http://localhost:8080/HELLO_WORLD) 中的地址
How to change address in url (http://localhost:8080/HELLO_WORLD) in NanoHttpd
我的问题是如何更改 URL (http://localhost:8080/HELLO_WORLD) 中的地址更改方式。我把HELLO_WORLD
改成了desire word.
@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- 创建一个本地来存储您的服务器文件;
2-然后将实现 NanoHttp 服务器的 class 中的响应更改为如下内容:
@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 - 然后,调用 localhost:8080 而不放置 8080/yourfolder/yourfile
我的问题是如何更改 URL (http://localhost:8080/HELLO_WORLD) 中的地址更改方式。我把HELLO_WORLD
改成了desire word.
@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- 创建一个本地来存储您的服务器文件; 2-然后将实现 NanoHttp 服务器的 class 中的响应更改为如下内容:
@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 - 然后,调用 localhost:8080 而不放置 8080/yourfolder/yourfile