I have an Android app with a webview and I need the Nanohhtd to have a webserver in my Android app.
Edited : All the files and the structure is saved in the internal storage of the application.
The response method of the server is responding the file that I call: the main html page.This is fine and the page is displayed on the webview, but the javascript does not work.
Instead of returning just the html page I am requiring, it is replacing all the code within the files that my page needs for the same code : the html content of the main webpage.
When I inspect the page, all the js files have the same content as the main html file.
For example:
main hmtl page - NanoHTTPD was supposed to return it.
<!DOCTYPE html>
<html>
<head>
<script src='sw/bootstrap/js/bootstrap.min.js'></script>
<script src="generator.js"></script>
<script src='sw/swipe.js'></script>
</head>
<body>
</body>
</html>
And the files in it was supposed to have their code
For example generator.js. instead of this :
function getAltura(alturaOriginal, larguraOriginal, larguraRedimensionada){
var n1 = alturaOriginal * larguraRedimensionada;
return n1 / larguraOriginal;
}
function gerarPagina(numPagina){
$(".swipe-wrap").append("<div id="+numPagina+"><img class='paginas' src='edicoes/"+raiz+"/paginas/pagina_"+numPagina+".png'> </div>");
}
function setLinkVideo(linkVideo){
document.getElementById('video-noticia').src = linkVideo;
}
generator.js has it :
<!DOCTYPE html>
<html>
<head>
<script src='sw/bootstrap/js/bootstrap.min.js'></script>
<script src="generator.js"></script>
<script src='sw/swipe.js'></script>
</head>
<body>
</body>
</html>
The same as the main page. And this happens to all files inside of the html page the nanohttp is reponding.
Finally, I use this code as response of the NanoHTTPD:
@Override
public Response serve(IHTTPSession session) {
String answer = "";
try{
FileReader filereader = new FileReader(contextoMain.getFilesDir()+ "/"+path);
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);
}
The path to the file in the android filesDir is correct. But what Am I doing wrong that Nanohttp is responding the same code to all files?
He was supposed to respond one file only.
Thank you in advance!
The Server Class - NanoHTTP server
import android.content.Context;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
/**
* Created by on 11/12/15.
* gerencia o webserver
*/
public class Myserver extends NanoHTTPD {
private final static int PORT = 8080;
Context contextoMain;
private String path;
public Myserver(Context cont, String _path) {
super(PORT);
contextoMain = cont;
this.path = _path;
try {
start();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println( "\nRunning! Point your browsers to http://localhost:8080/ \n" );
}
@Override
public Response serve(IHTTPSession session) {
String answer = "";
try{
//InputStreamReader input = new InputStreamReader(contextoMain.getAssets().open("test.html"));
FileReader filereader = new FileReader(contextoMain.getFilesDir()+ "/"+path);
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);
}