NanoHttp的使用入门

简介

NanoHTTPD是一个免费、轻量级的(只有一个Java文件) HTTP服务器,可以很好地嵌入到Java程序中。支持 GET, POST, PUT, HEAD 和 DELETE 请求,支持文件上传,占用内存很小。可轻松定制临时文件使用和线程模型。

使用

  1. 首先在build.gradle中添加依赖:
implementation 'org.nanohttpd:nanohttpd:2.3.1'

AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.INTERNET" />
  1. MyServer类:
package com.learn.learnnanohttp;

import android.util.Log;

import fi.iki.elonen.NanoHTTPD;

public class MyServer extends NanoHTTPD {
    //构造函数 赋值父类
    public MyServer(int port){
        super(port);
    }

    //重写Serve方法,每次请求时会调用该方法
    @Override
    public Response serve(IHTTPSession session) {
        Log.d("serve: ","enter");
        //获取请求uri
        String uri = session.getUri();

        //这里默认把接收到的uri返回
        return NanoHTTPD.newFixedLengthResponse(uri);
    }
}
  1. MainActivity类:
public class MainActivity extends AppCompatActivity {

    Button btn_getip;
    TextView tv_result;
    Context context = this;
    MyServer http = new MyServer(9988);

    //安卓创建事件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("服务启动中");
        try {
            http.start();
            System.out.println("服务启动完成");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服务启动错误");
        }

        tv_result = findViewById(R.id.tv_result);
        btn_getip = findViewById(R.id.btn_getip);
        btn_getip.setOnClickListener(new MyOnClickListener(tv_result,context));
        
    }

    //安卓销毁事件
    @Override
    protected void onDestroy() {
        super.onDestroy();
        http.stop();
    }

    static class MyOnClickListener implements View.OnClickListener {

        public final TextView tv_result;
        public final Context context;

        public MyOnClickListener(TextView tv_result,Context context){
            this.tv_result = tv_result;
            this.context = context;
        }

        @Override
        public void onClick(View v) {
            String ipAdress = getLocalIpStr(context);
            tv_result.setText(ipAdress);

        }

        public static String getLocalIpStr(Context context){
            WifiManager wifiManager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo=wifiManager.getConnectionInfo();
            return  intToIpAddr(((WifiInfo) wifiInfo).getIpAddress());
        }

        private static String intToIpAddr(int ip){
            return (ip & 0xFF)+"."
                    + ((ip>>8)&0xFF) + "."
                    + ((ip>>16)&0xFF) + "."
                    + ((ip>>24)&0xFF);
        }
    }


}
  1. activity_main.xml中加入以下布局:
    <Button
        android:id="@+id/btn_getip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击查看ip地址"
        />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示ip地址"
        android:textColor="@color/black"
        android:textSize="17sp"
        />

此时编译运行即可,点击查看ip地址后,在局域网内访问9988端口即可,如http://172.20.10.5:9988/

参考

NanoHttpd 轻量级的 HTTP 服务器
安卓NanoHTTPD的快速入门

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值