I'm using NanoHTTPd to serve files from my android app. I can open the .html files fine but it's trying to view images that's the problem. Anything like a webpages background image doesn't display.
Does anybody have any example code for this. I know nanoHTTPd can do this. I've lots of experience with Android and Java but this is the first time I've use a server.
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);
}
});
String html = null;
InputStream is = null;
if (uri.length() > 3) {
// respond with resource or sub page
// serve image?
if (uri.substring(uri.lastIndexOf("."), uri.length()).equals(".jpg")) {
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
Log.d("Serve", "image: " + Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
//serve page
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
Log.d("response subpage", Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
// respond with index
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile/" + "index.html");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("response index", Environment.getExternalStorageDirectory().getPath() + "/WiFile/" + "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 new NanoHTTPD.Response(HTTP_OK, MIME_HTML, html);
}
}
EDIT:
opening an image in the browser just returns lots of text symbols(...��k�OOO�...). Am I parsing the image the wrong way?
Fix:
Like Gustav said I wasn't using the corrent meme types but also I wasn't returning serveFile(.....) e.g.....
// serve image?
if (uri.substring(uri.lastIndexOf("."), uri.length()).equals(".jpeg")) {
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
mimeType = "image/jpeg";
Log.d("Serve", "image: " + Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
return serveFile(uri, header, new File(Environment.getExternalStorageDirectory().getPath() + "/WiFile"), true);
} catch (FileNotFoundException e) {}
} else {
//serve page
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
mimeType = MIME_HTML;
Log.d("response subpage", Environment.getExternalStorageDirectory().getPath() + "/WiFile" + uri);
return serveFile(uri, header, new File(Environment.getExternalStorageDirectory().getPath() + "/WiFile"), true);
} catch (FileNotFoundException e) {}
}
} else {
// respond with index
try {
is = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/WiFile/" + "index.html");
mimeType = MIME_HTML;
Log.d("response index", Environment.getExternalStorageDirectory().getPath() + "/WiFile/" + "index.html");
return serveFile(uri, header, new File(Environment.getExternalStorageDirectory().getPath() + "/WiFile"), true);
} catch (FileNotFoundException e) {}
}
You're passing MIME_HTML
to the Response
constructor regardless of what you're going to send as payload. The parameter is a String
, so try
return new NanoHTTPD.Response(HTTP_OK, "image/jpeg", html);
when serving (JPEG) images.