Here’s what happened. Some time ago when browsing Github, accidentally found an interesting project AndServer, said that it can use Android to write a runnable background service, and the writing method is also very similar to the current most popular SpringBoot, so I started to try, feel very interesting, so referring to the document, write a Server side and client side template, here to share to everyone, interested can play play, or very interesting.

AndServer

Before sharing, LET me briefly introduce the AndServer project.

AndServer is the open source project of Yanzhenjie, the boss of Alibaba. The project address is github.com/yanzhenjie/… , currently there is about 2.5K star, written relatively well, interested students can try to read the source code.

Introduction to the

The original description is as follows:

AndServer is an HTTP and reverse proxy server. Web server and Web framework of Android platform. It provides annotations like SpringMVC, and if you are familiar with SpringMVC, you can master it very quickly.

  • Static html website deployment.
  • Dynamic http api deployment.
  • Reverse proxy server.

Translation:

AndServer is an HTTP and reverse proxy server. Android Web server and Web framework. It provides comments like SpringMVC, which you can pick up quickly if you’re familiar with SpringMVC.

  • Static HTML web site deployment.
  • Dynamic HTTP API deployment.
  • Reverse proxy server.

Integrated AndServer

  • Add plugin to project buildScript:
buildscript { repositories { jcenter() mavenCentral() google() } dependencies { classpath 'com. Yanzhenjie. Andserver: plugin: 2.1.5'... }}...Copy the code
  • Add AndServer dependencies to your module:
apply plugin: 'com.yanzhenjie.andserver' ... Dependencies {implementation 'com. Yanzhenjie. Andserver: API: 2.1.5' annotationProcessor 'com. Yanzhenjie. Andserver: processor: 2.1.5'... }Copy the code

If you’re using Kotlin, replace the annotationProcessor with kapt.


TemplateAndServer

With AndServer out of the way, let’s move on to the TemplateAndServer I wrote.

TemplateAndServer is a simplified version of the Android server template that I wrote based on the AndServer documentation and examples, combined with the X-Library series of libraries I use frequently. It is mainly used for interface simulation testing and other scenarios.

Project address: github.com/xuexiangjys… , you can click star collection oh ~~


demo

Server demo

Browser demo

Writing contrast

  • AndServer

  • SpringBoot

As you can easily see from the image above, AndServer is written very similar to SpringBoot, even to the structure of the project.


Function is introduced

This template project mainly uses the services provided by AndServer to build, and has built-in common basic modules with the following functions.

  • Unified request logging.

  • Global exception capture processing, return uniform API results.

  • Added global permission verification interceptor.

  • File upload.

  • Background management interface.

  • Interface test interface.

Return Json format

{" code ": 0, / / the response code, 0 for success, or failure" MSG ":" ", / / request failure reasons "data" : {}} / / return data objectCopy the code

Integrated description

  • AndServer An HTTP and reverse proxy server
  • XHttp2 is a powerful network request library, using RxJava2 + Retrofit2 + OKHttp package.
  • XOrmlite a convenient and practical OrmLite database framework
  • XUI a simple and elegant Android native UI framework, free your hands!
  • XUtil a convenient and practical Android tool class library!
  • XAOP a lightweight AOP(Android) application framework. Covers the most useful AOP applications.
  • XPage is a very convenient fragment page framework.

use

Server Configuration

In com. Xuexiang. Server configuration changes ServerConfig.

Service interface writing

1. In com. Xuexiang. Server. The controller package created under the controller class.

2. Add @restController and @requestMapping annotations to the Controller class.

The log view

To view the request logs, search logcat for the keyword “AndServer”.


Wechat official account

More information, welcome to wechat search public number: [My Android open source journey]