采用NanoHTTPD框架
想明白原理的,请右转—>原文github地址 :https://github.com/NanoHttpd/nanohttpd

本文主要向您展示如何在Android应用程序中创建一个http服务器。

加入依赖包

   implementation 'org.nanohttpd:nanohttpd:2.3.1'

添加权限申请

                    

主界面程序

mport android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.widget.TextView;import java.io.IOException;import java.net.SocketException;public class MainActivity extends AppCompatActivity {    private MyAppServer mywebserver;    private TextView ipAddress;    @Override    public void onCreate( Bundle savedInstanceState ) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ipAddress = findViewById(R.id.ipAddress);        String localIPAddress = null;        try {            localIPAddress = GetLocalIp.getLocalIPAddress();        } catch (SocketException e) {            e.printStackTrace();        }        ipAddress.setText("当前设备ip地址为:"+localIPAddress+":33445");        Log.e("localIPAddress",localIPAddress+"");    }    @Override    public void onResume() {        super.onResume();        try {            mywebserver = new MyAppServer(this);            Log.e("onResume", "WebServer started");        } catch (IOException e) {            e.printStackTrace();            Log.e("onResume", "WebServer start failed" + e.getMessage());        }    }    @Override    public void onPause() {        super.onPause();        if (mywebserver != null) {            mywebserver.closeAllConnections();            mywebserver = null;            Log.e("onPause", "app pause, so web server close");        }    }}

server类

import android.content.Context;import java.io.IOException;import java.util.Map;import fi.iki.elonen.NanoHTTPD;public class MyAppServer extends NanoHTTPD {    private final static int PORT = 33445;//自定义    private Context _mainContext;    public MyAppServer(Context context) throws IOException {        super(PORT);        _mainContext = context;        start();        System.out.println("\nRunning! Point your browsers to [http://0.0.0.0:33445/](http://localhost:33445/)\n");    }    @Override    public Response serve(IHTTPSession session) {        String msg = "

Hello server

\n"; Map parms = session.getParms(); if (parms.get("username") == null) { msg += "
\n

Your name:

\n" + "
\n"; } else { msg += "

Hello, " + parms.get("username") + "!

"; } return newFixedLengthResponse(msg + "\n"); }}

获取本机ip

import java.net.Inet4Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.util.Enumeration;public class GetLocalIp {    /**     * 获取IP地址     * @return     * @throws SocketException     */    public static String getLocalIPAddress() throws SocketException {        for(Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){            NetworkInterface intf = en.nextElement();            for(Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){                InetAddress inetAddress = enumIpAddr.nextElement();                if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){                    return inetAddress.getHostAddress().toString();                }            }        }        return "null";    }}

成功在android设备中启动,获取IP后,即可在网站进行访问。

请求后的返回

更多相关文章

  1. adb devices 找不到设备的解决方法
  2. Android应用实例之----基于Service与ContentProvider的音乐播放
  3. android 借助AccessibilityService实现模拟点击功能-几个工具类(
  4. JS获取整个HTML网页代码 - Android(安卓)集美软件园 - 博客频道
  5. Android(安卓)Studio 常用插件收集
  6. android studio/idea各种坑
  7. android 应用如何获取系统权限 以及root系统方法
  8. javaeye中的一些好的android博客
  9. android获取各种系统路径的方法

随机推荐

  1. Android开发面试经——2.常见Android基础
  2. 经典的大牛博客推荐
  3. Android UI事件处理[isInTouchMode()]
  4. 带着问题学习 Android Handler 消息机制
  5. Android(安卓)铃音设置分析
  6. android ZygoteInit.java文件解析 从main
  7. Android ApiDemos 系列解析【View-ImageV
  8. Ubuntu通过MTP访问Android设备
  9. Android中圆角显示EditText,并且只能显示
  10. Android Makefile中如何识别TARGET_PRODU