Server side for android application in Java?

Question:

Good afternoon, dear developers. Faced the problem of choosing a way to implement the server side for an android application. The bottom line: writing a small application for the sake of learning. The application allows users to register and edit information about books (book library with CRUD functionality). The interaction will be implemented in HTTP. In order to study the implementation of client-server interaction, there is a desire for the server to choose java, but since there is no experience, I am also interested in your opinion on this issue. Is it possible to implement a server in java with servlets or do I have to write in Kotlin?

Answer:

Client is one piece (displays data)

The server is the other part (reading data from the database or other places)

The client can be any application that interacts with the server via the Internet (android application, any website or desktop application). The server does not depend on the client in any way. he only responds to requests from the client, and so on. the protocol is the same for everyone, then different applications can communicate with it.

A REST or SOAP service can be implemented as a server. In short, REST returns json as a response, and the soap service returns xml as a response (I know that using soap you can generate client code in java).

If you want to implement a service in java, then you can write it in both kotlin and groovy, since they are compatible with java (but mixing all 3 at once is not desirable).

Here is an example of a rest service on kotlin (you can probably find more modern examples) – https://auth0.com/blog/developing-restful-apis-with-kotlin/

Now about the client. On android, you can use the retrofit and okhttp3 libraries for requests to the REST service and the rxjava library so that the data can be loaded in a separate thread without slowing down the UI (so the UI then continues execution and as soon as the data is loaded, they will be displayed).

Below is an example of connecting to rest on android + kotlin

val client = OkHttpClient.Builder()
            .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
            .build()
val adapter = Retrofit.Builder()
            .baseUrl("http://localhost")
            .client(client)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(Gson()))
            .build()

authRepo = AuthRepository(adapter.create(
                AuthDataSource::class.java))


interface AuthDataSource {

    @FormUrlEncoded
    @POST("login/login")
    fun authenticate(@FieldMap params: Map<String, String>): Observable<User>

}

abstract class Repository {
    protected fun <T> applySchedulers(): Observable.Transformer<T, T> {
        return Observable.Transformer<T, T> { observable -> observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) }
    }
}

class AuthRepository(private val authDataSource: AuthDataSource) : Repository() {

    fun authenticate(login: String, password: String): Observable<User> {
        return authDataSource
                .authenticate(
                        mapOf(
                                "phone" to login,
                                "password" to password
                        )
                )
                .compose(this.applySchedulers<User>())
    }
Scroll to Top