android本地webServer使用(NanoHTTPD)

因为在开发android的html5应用的时候碰到了ajax跨域访问的问题,所有想到了在android本地开个webServer解决此问题,开源NanoHttpD帮我解决了这个问题,下面上连接

https://github.com/NanoHttpd/nanohttpd

主要代码部分

写道
package com.bluesky.cwan5games;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;

import android.content.res.AssetManager;
import android.util.Log;
import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Response.Status;

public class SimpleServer extends NanoHTTPD {
public AssetManager asset_mgr;

private static String TAG = SimpleServer.class.toString();

public SimpleServer() {
// 端口是8088,也就是说要通过http://127.0.0.1:8088来访当问
super(8088);
}

public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
Log.d(TAG, "SERVE :: URI " + uri);
final StringBuilder buf = new StringBuilder();
for (Entry<String, String> kv : header.entrySet())
buf.append(kv.getKey() + " : " + kv.getValue() + "\n");
InputStream mbuffer = null;
String file_name = uri.substring(1);
// 默认的页面名称设定为index.html
if (file_name.equalsIgnoreCase("")) {
file_name = "games/index.html";
} else {
file_name = "games/" + file_name;
}

try {
if (uri != null) {
if (file_name.contains(".js")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "application/javascript", mbuffer,
(long) mbuffer.available());
} else if (file_name.contains(".css")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "text/css", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".png")) {
mbuffer = asset_mgr.open(file_name);
// HTTP_OK = "200 OK" or HTTP_OK = Status.OK;(check comments)
return new NanoHTTPD.Response(Status.OK, "image/png", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".jpg") || file_name.contains(".jpeg")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "image/jpeg", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".ogv")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "video/ogg", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".ogg")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "application/x-ogg", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".txt")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "text/plain", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".xml")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "text/xml", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".json")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "text/json", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".mp3")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "audio/mpeg", mbuffer, (long) mbuffer.available());
} else if (file_name.contains(".mp4")) {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, "video/mp4", mbuffer, (long) mbuffer.available());
} else if (uri.contains("/mnt/sdcard")) {
// Log.d(TAG,"request for media on sdCard "+uri);
// File request = new File(file_name);
// mbuffer = new FileInputStream(request);
// FileNameMap fileNameMap = URLConnection.getFileNameMap();
// String mimeType = fileNameMap.getContentTypeFor(file_name);
//
// Response streamResponse = new Response(Status.OK, mimeType, mbuffer,
// (long)mbuffer.available());
// Random rnd = new Random();
// String etag = Integer.toHexString( rnd.nextInt() );
// streamResponse.addHeader( "ETag", etag);
// streamResponse.addHeader( "Connection", "Keep-alive");
// return streamResponse;
} else {
mbuffer = asset_mgr.open(file_name);
return new NanoHTTPD.Response(Status.OK, MIME_HTML, mbuffer, (long) mbuffer.available());
}
}
} catch (IOException e) {
Log.d(TAG, "Error opening file " + file_name +" "+ e.getMessage());
}
return null;
}
}

 这个项目还用了腾讯浏览器服务SDK,这个sdk比原生android的webview效率高,使用方便

https://x5.tencent.com/tbs/index.html

相关推荐