Как изменить адрес в URL (http://localhost:8080/HELLO_WORLD) в NanoHttpd
Мой вопрос заключается в том, как изменить, как изменить адрес в URL ( 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 ответ
Решение
Я не знаю, хотите ли вы этого, но вы можете попробовать. Вы должны следовать шагам:
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 - Затем позвоните localhost:8080, не помещая 8080/yourfolder/yourfile