AndServer的Github地址:https://github.com/yanzhenjie/AndServer,文档地址:https://yanzhenjie.com/AndServer/
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
。。。
classpath 'com.yanzhenjie.andserver:plugin:2.1.9' // AndServer
}
}
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'com.yanzhenjie.andserver' // AndServer
}
dependencies {
。。。
// AndServer
implementation 'javax.activation:javax.activation-api:1.2.0'
implementation 'com.yanzhenjie.andserver:api:2.1.9'
kapt 'com.yanzhenjie.andserver:processor:2.1.9'
}
注:这里的activation-api依赖在AndServer官网中并没有添加,因为官网的使用环境是jdk1.8,当我们的项目使用jdk11的时候,在processor库中的com.yanzhenjie.andserver.processor.ControllerProcessor类里面用到了javax.activation.MimeTypeParseException类,这个类在JDK1.8中是存在的,而在JDK11的时候就被删除掉了,在编译的时候会报如下警告:
Task :app:kaptDebugKotlin
[WARN] Can’t find annotation processor class com.yanzhenjie.andserver.processor.ControllerProcessor: javax/activation/MimeTypeParseException
编译报了这个警告,但程序可以正常运行,只是在客户端请求接口时就请求不到了,比如访问:http://192.168.1.75:8899/user/login接口,这个接口我们是已经做好了的,但是会提示找不到这个接口,如下:
The resource [/user/login] is not found.
所以,当我们使用JDK11或更高的JDK版本时,需要添加activation-api依赖。
相关实现类
import android.util.Log
import com.yanzhenjie.andserver.AndServer
import com.yanzhenjie.andserver.Server
import java.lang.Exception
import java.util.concurrent.TimeUnit
object AndServerEngine {
private var mAndServer: Server? = null
fun startup() {
if (mAndServer?.isRunning == true) {
Log.i("ABCD", "AndServer已经启动")
return
}
AndServer
.webServer(App.sContext)
.port(8899)
.timeout(10, TimeUnit.SECONDS)
.listener(object : Server.ServerListener {
override fun onStarted() {
Log.i("ABCD", "AndServer启动成功")
}
override fun onStopped() {
Log.i("ABCD", "AndServer关闭成功")
}
override fun onException(e: Exception?) {
Log.i("ABCD", "AndServer出现异常", e)
}
})
.build()
.apply {
mAndServer = this
startup()
}
}
fun shutdown() {
val server = mAndServer
if (server?.isRunning == true) {
server.shutdown()
mAndServer = null
} else {
Log.i("ABCD", "AndServer没有启动")
}
}
}
import android.util.Log
import com.yanzhenjie.andserver.annotation.RestController
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.GetMapping
import com.yanzhenjie.andserver.annotation.QueryParam
@RestController
@RequestMapping(path = ["/user"])
class UserController {
@GetMapping("/login")
fun login(@QueryParam("account") account: String?, @QueryParam("password") password: String?): String {
Log.i("ABCD", "收到登录请求,account = $account,password = $password")
return "Successful!"
}
}
这里,我们通过AndServerEngine来控制AndServer的启动和关闭,通过UserController来定义相关的请求接口,这里有一个需要注意的细节是,Get请求的参数注解是QueryParam,而Post请求的参数注解是RequestParam
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.startBtn).setOnClickListener { AndServerEngine.startup() }
findViewById<Button>(R.id.stopBtn).setOnClickListener { AndServerEngine.shutdown() }
}
}
最后,不要忘了在清单文件中设置一下网络权限,如下:
<uses-permission android:name="android.permission.INTERNET"/>
http://192.168.1.75:8899/user/login?account=ZhangShan&password=123456
效果如下: