1

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);
    }
Andressa Pinheiro
  • 1,517
  • 2
  • 18
  • 28
  • `He was supposed to respond one file only.` ?? What do you mean? I suppose your server should serve all requested files. Also the .js files. Are you saying the server should not be used for them? – greenapps Feb 18 '16 at 12:32
  • Where are you receiving the request for 'sw/bootstrap/js/bootstrap.min.js' ? Where are you receiving the request for the other two .js files? Where do you handle those requests? Show where 'path' is adapted in your code to server the requested file. – greenapps Feb 18 '16 at 12:35
  • I will edit the question. – Andressa Pinheiro Feb 18 '16 at 12:37
  • caminho == path ? Why are you posting the `serve` function twice? – greenapps Feb 18 '16 at 12:44
  • Yes. It is. I edited it again. I am trying to understand : How do I choose the file I want the httpd server to respond dinamically? For example; Can I do this in the browser : http://localhost:8080/file.html? I thought the server was suppose to respond just the file.html and browser handles the rest. – Andressa Pinheiro Feb 18 '16 at 12:59
  • You do not choose the file. The browser requests a file. Your server gets the request. You have to look in the request to see which file the browser is requesting. You are not doing that. Once you know which file the browser requests you (the server) can try to find it and adapt `path`acordingly. No the browser cannot handle the other files as where would the browser get them from? – greenapps Feb 18 '16 at 13:07
  • `Can I do this in the browser : localhost:8080/file.html?` Yes `http://localhost:8080/file.html` or `http://127.0.0.1:8080/file.html`. But only in a browser of the same device where your server is running on. What did you use before ? – greenapps Feb 18 '16 at 13:09
  • Yes, I use the browser in the same device as the server. How should be the response method in my server class if I use http://localhost:8080/file.html? Because I didn´t see any example like this. I saw examples where I set the browser to http://localhost:8080 and in the response method I choose the file to be sent(as my example above); – Andressa Pinheiro Feb 18 '16 at 13:38
  • As said several times before: you have to look at the request. So somewhere you will see `file.html` or `generator.js`. Where? Well look at the `session` parameter of `Response serve(IHTTPSession session)`. Your IDE will tell you a lot of usable properties and methods if you use it well. – greenapps Feb 18 '16 at 14:01
  • I wonder why this is all a problem for you as nano comes with a lot of examples for serving files. – greenapps Feb 18 '16 at 14:04
  • Please, give me the link where I can find these lots of examples for serving files. Sorry, the files are saved in the internal storage of the application. – Andressa Pinheiro Feb 18 '16 at 14:40
  • `Sorry, the files are saved in the internal storage of the application`. Why are you telling that? What does it matter? Your concern first is to determine wnich file is requested by the browser. Apparently you never googled for nanohttpd examples. – greenapps Feb 18 '16 at 14:46
  • 1
    Have a look at `session.getUri()`. This is one of the functions your IDE would tell you if you just typed `session.`. – greenapps Feb 18 '16 at 14:48
  • It´s exactly what I just did. And Now I can fix it. It was returning the same file to all of the others because I passed just one. I thought the browser would handle it. And I googled for nanohttpd examples. you are trying to help, but with this rudeness? – Andressa Pinheiro Feb 18 '16 at 14:53

1 Answers1

-1

Finally, I use this code as response of the NanoHTTPD. Well you should have started with that. And important info is missing as what is the value in path ? And where can we see that you look which file is requested? You are not doing that. You only server the file in path which is always the same as where would you have changed it?

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • I put it at the end because I had to explain what was happening first. You would not understand. Anyway, I receive the path of the file in the constructor. The path is correct. I alread checked. The page is loaded , but the js files have the same content. I get the file I want to respond through this : FileReader filereader = new FileReader(contextoMain.getFilesDir()+ "/"+path); – Andressa Pinheiro Feb 18 '16 at 12:35
  • `I receive the path of the file in the constructor.`. What do you mean? Receive from who? – greenapps Feb 18 '16 at 12:37
  • You do not have to write that statement again here. You were asked to tell where you adapt path. You talk about a file but the browser will request four files from your server. The original .html and then three .js files. – greenapps Feb 18 '16 at 12:39