Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android NanoHTTPD正在对网页中的所有文件响应相同的文件_Android_Html_Webview_Httpwebresponse_Nanohttpd - Fatal编程技术网

Android NanoHTTPD正在对网页中的所有文件响应相同的文件

Android NanoHTTPD正在对网页中的所有文件响应相同的文件,android,html,webview,httpwebresponse,nanohttpd,Android,Html,Webview,Httpwebresponse,Nanohttpd,我有一个带有webview的Android应用程序,我需要Nanohhtd在我的Android应用程序中有一个Web服务器 已编辑:所有文件和结构都保存在应用程序的内部存储器中 服务器的响应方法是响应我调用的文件:主html页面。这很好,页面显示在webview上,但javascript不起作用 它不是只返回我需要的html页面,而是替换我的页面需要的文件中的所有代码,以获得相同的代码:主网页的html内容 当我检查页面时,所有js文件都与主html文件具有相同的内容 例如: hmtl主页-Na

我有一个带有webview的Android应用程序,我需要Nanohhtd在我的Android应用程序中有一个Web服务器

已编辑:所有文件和结构都保存在应用程序的内部存储器中

服务器的响应方法是响应我调用的文件:主html页面。这很好,页面显示在webview上,但javascript不起作用

它不是只返回我需要的html页面,而是替换我的页面需要的文件中的所有代码,以获得相同的代码:主网页的html内容

当我检查页面时,所有js文件都与主html文件具有相同的内容

例如:

hmtl主页-NanoHTTPD应该会退回它

<!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>
android FileDir中文件的路径是正确的。 但是Nanohttp对所有文件都响应相同的代码,我做错了什么

他应该只回复一份文件

提前谢谢你

服务器类-NanoHTTP服务器

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);
    }

最后,我使用此代码作为NanoHTTPD的响应。你应该从这个开始。缺少重要信息,因为路径中的值是多少?我们在哪里可以看到您查看请求的文件?你没有那样做。您只在
路径
中提供文件,该路径始终与您更改文件的位置相同。

他应该只响应一个文件。
??什么意思?我想您的服务器应该提供所有请求的文件。还有.js文件。你是说服务器不应该用于他们吗?你在哪里收到“sw/bootstrap/js/bootstrap.min.js”的请求?您在哪里收到对另外两个.js文件的请求?你在哪里处理这些请求?显示“路径”在代码中的位置,以便为请求的文件提供服务器。我将编辑该问题。caminho==路径?为什么要发布两次
service
函数?请查看
session.getUri()
。这是IDE将告诉您的函数之一,如果您只是键入
会话。
。我将其放在末尾,因为我必须解释首先发生的事情。你不会明白的。无论如何,我在构造函数中收到了文件的路径。这条路是正确的。我已经看过了。页面已加载,但js文件的内容相同。我通过以下方式得到我想要响应的文件:FileReader FileReader=newfilereader(contextoMain.getFilesDir()+“/”+path)<代码>我收到构造函数中文件的路径。
。什么意思?从谁那里收到?你不必再在这里写那份声明。你被要求告诉你在哪里适应这条路。您谈论一个文件,但浏览器将从您的服务器请求四个文件。原始的.html文件,然后是三个.js文件。
 <!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>
@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);
}
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);
    }