搭建本地httpserver

使用NanoHTTPD搭建android本地httpserver

搭建本地httpserver是为了解决android不支持本地ajax访问(当然可以通过对webview的设置也可以支持本地ajax访问,见文章末尾)

NanoHTTPD架包下载地址

1. 启动httpserver

// 在一个子线程中启动这个httpserver
mhttpserver = HttpServer.getHttpServer(8080, MainActivity.this);
try {
                    mhttpserver.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

2. 销毁httpserver

@Override
    protected void onDestroy() {
        super.onDestroy();
        if (mhttpserver != null) {
            mhttpserver.stop();
        }
    }

3. httpserver实例

public class HttpServer extends NanoHTTPD {

    private static HttpServer httpServerinstance = null;

    public static HttpServer getHttpServer(int port, Context context) {
        if (null == httpServerinstance) {
            httpServerinstance = new HttpServer(port, context);
        }
        return httpServerinstance;
    }

    private HttpServer(int port, Context context) {
        super(port);
    }

    @Override
    public Response serve(IHTTPSession session) {

        Log.i("lsw", session.getUri());
        return responseCourseStream(session);
    }


    public Response responseCourseStream(IHTTPSession session) {

        String uri = session.getUri();
        String mime = getMimeTypeForFile(uri);
        Log.i("lsw", "mime:" + mime);
        uri = uri.substring(1);
        try {
//            InputStream is = mContext.getResources().getAssets().open(uri);
// 根据不同的路径进行修改
            FileInputStream is = new FileInputStream(Environment.getExternalStorageDirectory() + "/ubcoll/game/" + uri);
            return newChunkedResponse(Response.Status.OK, mime, is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newFixedLengthResponse("404");
    }
}

4. webview调用

// path 需与3中的路径连接起来,连成完整路径即可
// 比如这里的path只传index.html,3中的路径就要指到index.html父目录的路径为止
wv.loadUrl("http://localhost:8080/" + path);

5. 设置webview,支持本地ajax访问

//解决XMLHttpRequest cannot load file from android asset folder
ws.setJavaScriptEnabled(true);
ws.setPluginState(PluginState.ON);
ws.setAllowFileAccess(true);
ws.setAllowContentAccess(true);
ws.setAllowFileAccessFromFileURLs(true);
ws.setAllowUniversalAccessFromFileURLs(true);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值