当前位置:   article > 正文

Android实现WebServer(服务端)之AndServer_android andserver

android andserver

作者:小红妹
开发工具:Android studio
开发语言:Kotlin+Java
Gradle版本:gradle-7.5

大开眼界,Android竟然也可以写应用服务端了

前言

------最近公司有个大胆的需求,让Android端提供WebServer服务(需要写接口),把服务封装在一个本地应用中,供第三方调用,比如另外一个客户端调用,或者浏览器访问等等。
------啊这!安卓可以写后端吗?以前想都没想过这种场景,后端一般都是有后端开发人员写,使用Java、PHP或C#等等,Android?能吗?好风凭借力,利用第三方插件,也可以起飞~
------突然发现,自己对网络协议这块很懵懂,什么HTTP、TCP、三次握手四次挥手、WebSocket协议、网口通信、IP地址和网口等等,晕~
能咋搞,学起来~

集成

使用AndServer搭建Android平台的Web服务器。

GitHub地址:https://github.com/yanzhenjie/AndServer/
使用指南:https://yanzhenjie.github.io/AndServer/
  • 1
  • 2

1、将插件添加到项目构建脚本中

buildscript {
   
    repositories {
   
        google()
        mavenCentral()
    }

    dependencies {
   
        classpath 'com.yanzhenjie.andserver:plugin:2.1.11'
        ...
    }
}

allprojects {
   
    repositories {
   
        google()
        mavenCentral()
    }
}
...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2、然后将AndServer依赖项添加到模块中

apply plugin: 'com.yanzhenjie.andserver'

...

dependencies {
   
    implementation 'com.yanzhenjie.andserver:api:2.1.11'
    annotationProcessor 'com.yanzhenjie.andserver:processor:2.1.11'
    ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

如果使用Kotlin,请用kapt替换annotationProcessor。

集成遇到的问题
使用Android studio最新版本,gradle-7.5版本,Kotlin语言,死活依赖不成功,gradle版本7以后,build.gradle发生了变化。下面是添加依赖的样子。
1、将插件添加到项目构建脚本中

plugins {
   
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
    id 'com.yanzhenjie.andserver'
}
...

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2、然后将AndServer依赖项添加到模块中

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
   
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    id 'com.yanzhenjie.andserver' version '2.1.11' apply false
}

task clean(type: Delete) {
   
    delete rootProject.buildDir
}
...
dependencies {
   

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'com.yanzhenjie.andserver:api:2.1.11'
    annotationProcessor 'com.yanzhenjie.andserver:processor:2.1.11'

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

一直依赖失败,报错误

Plugin [id: 'com.yanzhenjie.andserver'] was not found in any of the following sources:
Plugin [id: 'com.yanzhenjie.andserver', version: '2.1.11', apply: false] was not found in any of the following sources:
  • 1
  • 2

好吧!后面改用以前老项目集成,竟然可以了,各个版本都低,暂且拿老项目来改改使用在这里插入图片描述
大家可以集成试试。

代码展示

1、AppConfig

import android.content.Context;
import com.yanzhenjie.andserver.annotation.Config;
import com.yanzhenjie.andserver.framework.config.Multipart;
import com.yanzhenjie.andserver.framework.config.WebConfig;
import com.yanzhenjie.andserver.framework.website.AssetsWebsite;
import java.io.File;

/**
 * @author 小红妹
 * @date 2023/2/24
 * @email L2279833535@163.com
 * @package com.youqian.paintingboard.web
 * @describe
 * @copyright
 */
@Config
public class AppConfig implements WebConfig {
   

    @Override
    public void onConfig(Context context, Delegate delegate) {
   
        // 增加一个静态网站
        delegate.addWebsite(new AssetsWebsite(context, "/web"));

        // 自定义配置表单请求和文件上传的条件
        delegate.setMultipart(Multipart.newBuilder()
                .allFileMaxSize(1024 * 1024 * 20) // 单个请求上传文件总大小
                .fileMaxSize(1024 * 1024 * 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/780077
推荐阅读
相关标签
  

闽ICP备14008679号