2013-03-09 39 views
0

我正在使用NanoHTTPD从Android本地托管网页。NanoHTTPD未显示其他页面

我的问题是,服务器响应我的网站的索引页,但我不知道如何导航到任何页面过去,因为它总是会回应索引页。

任何人都有任何教程,我正在努力寻找任何东西。谢谢:)

private class MyHTTPD extends NanoHTTPD { 
    public MyHTTPD() throws IOException { 
     super(PORT, null); 
    } 

    @Override 
    public Response serve(String uri, String method, Properties header, Properties parms, Properties files) { 
     Log.d("response", "URI:" + uri + " method: " + method + " header: " + header + " parms: " + parms + " files: " + files); 
     final StringBuilder buf = new StringBuilder(); 
     for (Entry<Object, Object> kv : header.entrySet()) 
      buf.append(kv.getKey() + " : " + kv.getValue() + "\n"); 
     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       hello.setText(buf); 
      } 
     }); 
     //load the index page 
     String html = null; 
     InputStream is = getClass().getResourceAsStream("/com/me/pages/index.html"); 
     byte[] b; 
     try { 
      b = new byte[is.available()]; 
      is.read(b); 
      html = new String(b); 
     } catch (IOException e) { // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

        //return index as response 
     return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, html); 
    } 
} 
+0

只要有人在意,我将URI附加到输入流的末尾。不错,简单:) – user1056798 2013-03-10 13:41:17

回答

0

NanoHTTPD不显示其他页面

当然,它不会显示别人,作为你的叫小serve功能仅仅是为/com/me/pages/index.html文件。对于其他文件(如您所说的页面),需要将URI映射到要提供的相应文件。

例如,您可以按照不同程序员在github上开发NanoHTTPD。网络图是在这里,一个可以帮助你的情况(服务多个页面)的特殊分支是here

+0

要更新这些链接,现在可以在https://github.com/NanoHttpd/nanohttpd找到NanoHttpd - 正在尝试使用它的不同人的网络位于https://github.com/NanoHttpd/nanohttpd/network - “简单Web服务器”示例可能是针对多个文件服务的一个很好的案例研究。这可以在https://github.com/NanoHttpd/nanohttpd/blob/master/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java找到。 – 2013-09-06 12:32:38

相关问题