Android设备搭建http服务---------AndServer

这篇具有很好参考价值的文章主要介绍了Android设备搭建http服务---------AndServer。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

项目中引用AndServer
implementation 'com.yanzhenjie.andserver:api:2.1.10'

在项目的根build.gradle文件(不是app那个moudle的build.gradle)最顶部添加:

buildscript {
    repositories {
        mavenCentral()
    }
 
    dependencies {
        classpath 'com.yanzhenjie.andserver:plugin:2.1.10'
    }
}
在当前的moudle(一般就是app)的build.gradle里面的plugins里面添加:
id 'com.yanzhenjie.andserver'
plugins
plugins {
    id 'com.android.application'
    id 'com.yanzhenjie.andserver'
}
build.gradle里面dependencies的添加
    implementation 'com.yanzhenjie.andserver:api:2.1.10'
    annotationProcessor 'com.yanzhenjie.andserver:processor:2.1.10'
代码
package com.dzdpencrypt.dzdp;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
 
import com.yanzhenjie.andserver.AndServer;
import com.yanzhenjie.andserver.Server;
 
import java.util.concurrent.TimeUnit;
 
public class MainActivity extends AppCompatActivity {
    private Server mServer;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!isServiceRunning(getApplicationContext(), KeepAliveService.class)) {
            Log.d(LOG_TAG, "检测到服务未在运行,启动服务");
            serviceIntent = new Intent(this, KeepAliveService.class);
            startService(serviceIntent);
        } else {
            Log.d(LOG_TAG, "检测到服务正在运行,无需再次启动");
        }
        
        TextView textView = findViewById(R.id.ipsd);   # 绑定textview的相关id
        textView.setText(NetUtils.getLocalIPAddress().getHostAddress() + ":9999");   # app中textview显示ip+端口
        mServer = AndServer.webServer(this)
                .port(9999)
                .timeout(10, TimeUnit.SECONDS).listener(new Server.ServerListener() {
                    @Override
                    public void onStarted() {

                        System.out.println("服务器绑定地址:"+NetUtils.getLocalIPAddress().getHostAddress());
                    }

                    @Override
                    public void onStopped() {

                    }

                    @Override
                    public void onException(Exception e) {

                    }
                })
                .build();

        mServer.startup();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mServer.shutdown();
    }
}

以上代码启动在Android手机(设备)上的http服务器,服务器绑定端口9999文章来源地址https://www.toymoban.com/news/detail-753554.html

spring样式的restful代码实现
package com.dzdpencrypt.service;
 
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.PathVariable;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.QueryParam;
import com.yanzhenjie.andserver.annotation.RequestBody;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.RestController;
 
import org.json.JSONObject;
 
 
@RestController
public class ServerController {
    @GetMapping("/")
    public String ping() {
        return "SERVER OK";
    }
 
    @PostMapping("/user/login")
    public JSONObject login(@RequestBody String str) throws Exception {
        JSONObject jsonObject = new JSONObject(str);
        return jsonObject;
    }
 
    @GetMapping("/user/item")
    public JSONObject requestItem(@RequestParam("name") String name,
                                 @RequestParam("id") String id) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("id", id);
 
        return jsonObject;
    }
 
    @GetMapping("/user/{userId}")
    public JSONObject getUser(@PathVariable("userId") String userId,
                           @QueryParam("key") String key) throws Exception{
        JSONObject user = new JSONObject();
        user.put("id", userId);
        user.put("key", key);
        user.put("year", 2022);
 
        return user;
    }
}
NetUtils.java-------获取当前Android手机的IP地址
package com.dzdpencrypt.service;
 
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Pattern;
 
public class NetUtils {
    private static final Pattern IPV4_PATTERN = Pattern.compile(
            "^(" + "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" +
                    "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
 
    public static boolean isIPv4Address(String input) {
        return IPV4_PATTERN.matcher(input).matches();
    }
 
    public static InetAddress getLocalIPAddress() {
        Enumeration<NetworkInterface> enumeration = null;
        try {
            enumeration = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        if (enumeration != null) {
            while (enumeration.hasMoreElements()) {
                NetworkInterface nif = enumeration.nextElement();
                Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
                if (inetAddresses != null) {
                    while (inetAddresses.hasMoreElements()) {
                        InetAddress inetAddress = inetAddresses.nextElement();
                        if (!inetAddress.isLoopbackAddress() && isIPv4Address(inetAddress.getHostAddress())) {
                            return inetAddress;
                        }
                    }
                }
            }
        }
        return null;
    }
}
AndroidManifest.xml配置网络权限
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
测试
  • get测试
    android做http服务器,android,工具,Android逆向,android,http,服务器
  • post测试
  • android做http服务器,android,工具,Android逆向,android,http,服务器
    可结合https://blog.csdn.net/zyc3545/article/details/109150010此篇文章合并到一块

到了这里,关于Android设备搭建http服务---------AndServer的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python搭建HTTP服务器

    项目经常需要HTTP对接,模拟HTTP client请求可以使用postman测试,模拟HTTP server回复该如何处理?本文介绍通过python搭建HTTP服务器的过程。 先装python,然后装PyCharm。 python安装、下载说明,看这部分 https://www.runoob.com/python3/python3-install.html PyCharm安装、下载说明,看这部分 PyCharm

    2024年02月05日
    浏览(34)
  • HFS 快速搭建 http 服务器

    HFS 是一个轻量级的HTTP 服务工具,3.0版本前进提供Windows平台安装包,3.0版本开提供Linux和macOS平台的安装包。 HFS更适合在局域网环境中搭建文件共享服务或者安装配置源服务器。 甲 非守护进程的方式运行 HFS (Ubuntu 22.04) 一 创建 HTTP 的根目录 /home/budgie/mirrorsHTTP mkdir -vp /ho

    2024年04月12日
    浏览(31)
  • 一条命令搭建HTTP服务器

    转载自远程内网穿透的文章:【Python】快速简单搭建HTTP服务器并公网访问「cpolar内网穿透」 Python作为热度比较高的编程语言,其语法简单且语句清晰,而且python有良好的兼容性,可以轻松的和其他编程语言((比如C/C++))建立的模块连接起来,而且python丰富强大的库,经过封

    2024年02月01日
    浏览(103)
  • windows环境(本地端以及华为云服务器)搭建HTTP服务器

    最近在调试一款中移物联网推出的NB-IOT物联网模组,模组有个功能是需要实现固件在线下载,那么模组更新固件的时候可以通过服务器端通过HTTP协议进行下载,因此首先需要搭建一个HTTP服务器。 本篇文章从本地电脑端以及华为云服务器端分别进行了HTTP服务器的搭建,并实现

    2024年02月15日
    浏览(35)
  • 搭建自己的MQTT服务器,实现设备上云(Ubuntu+EMQX)

    这篇文章教大家在ECS云服务器上部署EMQX,搭建自己私有的MQTT服务器,配置EMQX实现设备上云,设备数据转发,存储;服务器我采用的华为云的ECS服务器,系统选择Ubuntu系统。 Windows版本的看这里: https://blog.csdn.net/xiaolong1126626497/article/details/134280836 EMQX是一款大规模可弹性伸缩

    2024年02月04日
    浏览(43)
  • 申请阿里云服务器并搭建公网可支持数据上传的HTTP服务器

            拥有一台自己的云服务器可以做很多事情。阿里云服务器毫无疑问是国内最好的。         阿里云服务器可以用于各种互联网应用的搭建和运行,提供稳定、高性能的服务。         阿里云服务器的用途,包括但不限于以下几个方面: 网站托管:可以将网站

    2024年02月16日
    浏览(61)
  • 超简单--搭建http、https代理服务器

    vim /etc/squid/squid.conf systemctl start squid systemctl status squid systemctl enable squid 日志位置 /var/log/squid 服务器搭建完成 linux主机配置 //编辑配置文件 vi /etc/profile //在该配置文件的最后添加代理配置 // 退出profile文件并保存 source /etc/profile // 使配置文件生效 普通PC电脑 直接在浏览器或网

    2024年02月08日
    浏览(37)
  • Node.js怎么搭建HTTP服务器

    在 Node.js 中搭建一个简单的 HTTP 服务器非常容易。以下是一个基本的示例,演示如何使用 Node.js 创建一个简单的 HTTP 服务器: // 导入 http 模块 const http = require(\\\'http\\\'); // 创建一个 HTTP 服务器 const server = http.createServer((req, res) = { // 设置响应头 res.writeHead(200, {\\\'Content-Type\\\': \\\'text/pl

    2024年02月10日
    浏览(37)
  • Go Fiber搭建一个HTTP服务器

    Fiber 是一个 Express 启发 web 框架基于 fasthttp ,最快 Go 的 http 引擎。设计为简易,及快速的方式开发,同时考虑零内存分配和性能。这里默认你已经搭建好了本地Go环境。 一、安装 二、创建本地工程 创建本地工程后,使用 go mod init 初始化当前文件夹为一个 Go Module,并指定其导

    2024年02月09日
    浏览(32)
  • linux:http服务器搭建及实验案例

    1,安装http服务 2,将 /etc/selinux/config 文件下面的 SELINUX值 改为 disabled 或者 permissive 。 3,关闭防火墙 systemctl stop firewalld 做上面的工作是为了http在提供服务时让其不会阻止读取一些文件。 /etc/httpd/ 里面是http的主要的配置文件 tree /etc/httpd/ 可以看到这个文件的结构一目了然

    2024年02月09日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包