I am trying to use NanoHTTP to serve up an HTML file with help of Android. Currently i'm able to do display the page. I'm also new to andriod. The issue is not having a clear picture how to refer the images and css for that html file even after i have taken the files with help of AssetManager. Below i provided the entire code. Could somebody help me on this.
Note: Other permission settings are fine like InternetPermission. Only not having an idea how refer the other supported files (css, images, javascript files) for the html.
package com.web;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.*;
import java.util.*;
public class MainActivity extends Activity
{
private WebServer server;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AssetManager assetManager = getAssets();
String imagePath = "fileapp/image";
String cssPath = "fileapp/css";
String files[] = null;
try {
files = assetManager.list(imagePath);
Log.i("Image", files[0]);
Log.w("Image", files[0]);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Image ",e.getMessage());
}
server = new WebServer();
try {
server.start();
} catch(IOException ioe) {
Log.w("Httpd", "The server could not start.");
}
Log.w("Httpd", "Web server initialized.");
}
// DON'T FORGET to stop the server
@Override
public void onDestroy()
{
super.onDestroy();
if (server != null)
server.stop();
}
private class WebServer extends NanoHTTPD {
public WebServer()
{
super(8080);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header,
Map<String, String> parameters,
Map<String, String> files) {
StringBuilder msg = new StringBuilder("<html><head><title>" + heading + "</title>" +
**"<link href=\"../../../fileapp/css/style.css\" rel=\"stylesheet\">" +**
"</head>"
+ "<body>");
msg.append("**<div class=\"jumbotron-sm\"> <img src=\"../../../fileapp/image/logo.png\">**</div>");
msg.append("<div>My Body Content go here......</div>");
msg.append("</body>"
+ "</html>");
return msg.toString();
}
}
}
Kindly let me know in case of any further details.
Thanks advance for your answers.