Exploration of the high-speed development framework of the Android Project (Mysql + OrmLite + Hessian + SQL

Source: Internet
Author: User
The quick development framework of the Android Project (Mysql + OrmLite + Hessian + Sqlite) preface combines the previously used ormlite and hessian. In addition, SAE supports JAVA and switches the server to JAVA, this article is also available. The use of hessian for data transmission, ormlite for client and server data storage, greatly reducing CRUD

The quick development framework of the Android Project (Mysql + OrmLite + Hessian + Sqlite) preface combines the previously used ormlite and hessian. In addition, SAE supports JAVA and switches the server to JAVA, this article is also available. The use of hessian for data transmission, ormlite for client and server data storage, greatly reducing CRUD

Exploration of the Android project rapid development framework (Mysql + OrmLite + Hessian + Sqlite)

Preface

In combination with the previously used ormlite and hessian, and SAES that support JAVA, switch the server to JAVA. The use of hessian for data transmission, ormlite for client and server data storage, greatly reducing the CRUD work. This article is an exploration post, which is not officially used for large-scale projects. You are welcome to discuss it!

?

Statement
You are welcome to repost, but please keep the original source of the article :)
ITEYE: http://www.iteye.com/
Farmer UNCLE: http://www.cnblogs.com/over140?

?

Body

I. Introduction

1.1 ormlite

Ormlite [Object Relational Mapping Lite (ORM Lite)]
The object link ing Lite version (the simplified ORM) provides some simple, lightweight, persistent Java objects to the SQL database, while also avoiding complexity and more standard ORM package overhead features.

It supports jdbc calls of databases. Of course, the most important thing is that it supports the android native database api to call sqlite.

-- Reprinted from here .?

1.2 hessian?

Refer to the following two articles:

[Hessdroid] Is Hessian used in Android to communicate with the Java server?

[Hessdroid] In Android, how does Hessian communicate with the Java server?

?

1.3Android quick development framework

Consider the following features:

A) Does the client (Android) and server both use the Java language?

B) the client (Android) and server support Hessian and ormlite frameworks.

C). complete support for object-oriented development: storage and interactive transmission?

?

2. Preparation

2.1 Development Environment

To facilitate simultaneous development of Android and Java Web, the Eclipse IDE for Java EE Developers version is downloaded here, and the latest ADT plug-in and TOMCAT plug-in are installed.

2.2 Server

The application server uses Tomcat and Java (JSP/Servlet) to implement the service logic of the server. The database uses Mysql. We recommend that you use XAMPP for quick framework setup (integrated with Apache, MySQL, PHP, etc., supports green installation ).

2.3 Client

Normal Android Environment

2.4 communication and storage

The server and the client exchange data through Hessian and store the database through Ormlite (the MYSQL database saved to the server through JDBC or the sqlite database of the client );

?

Iii. Code

3.1 Project (server)

? HOLib is used both on the client and server to ensure interface and data object consistency.

?

3.2 key code analysis

3.2.1 Server

Web. xml


???? Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee? Web-app_2_4.xsd"
???? Version = "2.4">

????
???????? User
???????? Com. nmbb. ho. server. servlet. UserServlet
????

????
???????? User
???????? /User. do
????
????
????
???????? Com. nmbb. ho. server. StartupInit
????

?

StartupInit. java

Public? Class? StartupInit? Implements? ServletContextListener? {

???? @ Override
???? Public? Void? ContextInitialized (ServletContextEvent? Arg0 )? {
???????? Try? {
???????????? TableUtils. dropTable (OrmliteHelper. getConnection (),? POUser. class,
???????????????????? True );
???????????? // Create a database
???????????? TableUtils. createTable (OrmliteHelper. getConnection (),? POUser. class );
????????}? Catch? (SQLException? E )? {
???????????? E. printStackTrace ();
????????}
????}

???? @ Override
???? Public? Void? ContextDestroyed (ServletContextEvent? Arg0 )? {

????}

}

?? Code Description:

StartupInit can be used to create a database table structure, which is used for testing. Pay attention to data loss in the real environment.

POUser. java

@ DatabaseTable (tableName? =? "Nmbb_users ")
Public? Class? POUser? Implements? Serializable? {

???? /**? User ID, 6 digits? */
???? @ DatabaseField (generatedId? =? True)
???? Public? Int? Suid;

???? /**? Username? */
???? @ DatabaseField (width? =? 30)
???? Public? String? Username;

???? /**? Password? */
???? @ DatabaseField (width? =? 30)
???? Public? String? Password;

???? /**? Nickname? */
???? @ DatabaseField (width? =? 60)
???? Public? String? Nickname;

???? /**? 200? Normal? 201? Data validation error? 202 The user already exists? */
???? Public? Int? Status? =? 200;

???? /**? Used to put error messages? */
???? Public? String? Msg;

???? Public? POUser ()? {

????}
}

? Code Description:

Note that an empty constructor is required. For more information, see ormlite .?

?

UserServlet. java?

/**
? *? User Servlet
? *?
? *? @ Author? Farmer's uncle
? *? @ See? Http://www.cnblogs.com/over140/archive/2013/02/19/2917231.html
? *
? */
Public? Class? UserServlet? Extends? HessianServlet? Implements? IUserService? {

???? @ Override
???? Public? POUser? Register (String? Username ,? String? Password )? {
???????? POUser? Result? =? New? POUser ();

???????? System. out. println ("[UserServlet. register]...");

???????? //? Check whether the data is valid
???????? If? (IsEmpty (username )? |? IsEmpty (password ))? {
???????????? Result. status? =? 201;
???????????? Result. msg? =? "The user name or password cannot be blank ";
????????}? Else? {
???????????? //? Check whether the user exists
???????????? OrmliteHelper ? Db? =? New? OrmliteHelper ();
???????????? If? (Db. exist (POUser. class ,? "Username ",? Username ))? {
???????????????? Result. status? =? 202;
???????????????? Result. msg? =? "The user name already exists ";
????????????}? Else? {
???????????????? Result. username? =? Username;
???????????????? Result. password? =? Password;
???????????????? Db. create (result );//? Warehouse receiving
???????????????? Result. msg? =? "Registration successful ";
???????????????? System. out. println ("create? User? Suid :"? +? Result. suid );
????????????}
????????}
???????? Return? Result;
????}

???? @ Override
???? Public? List ? Query (int? Suid ,? Int? StartIndex ,? Int? PageSize )? {
???????? Return? New? OrmliteHelper (). Query (POUser. class ,? "Suid ",? Suid ,? StartIndex ,? PageSize )?;
????}

???? /**
????? *? Determines whether the string is null.
????? *?
????? *? @ Param? Str
????? *? @ Return
????? */
???? Public? Static? Boolean? IsEmpty (String? Str )? {
???????? Return? Str? ==? Null? |? Str. length ()? ==? 0;
????}
}

?

3.2.2 client (Android )??

Public? Class? UserActivity? Extends? Activity? {

???? @ Override
???? Protected? Void? OnCreate (Bundle? SavedInstanceState )? {
???????? Super. onCreate (savedInstanceState );
???????? SetContentView (R. layout. main );
????}

???? Public? Void? OnClickRegiger (View? View )? {
???????? New? AsyncTask ()? {

???????????? @ Override
???????????? Protected? POUser? DoInBackground (Void ...? Params )? {
???????????????? String? Url? =? "Http: // 192.168.68.23: 8081/HOServer/user. do ";
???????????????? HessianProxyFactory? Factory? =? New? HessianProxyFactory ();
???????????????? Try? {
???????????????????? Factory. setDebug (true );
???????????????????? Factory. setReadTimeout (5000 );
???????????????????? // If this parameter is not set, a message is returned? Expected? Hessian? Reply? At? 0x48?
???????????????????? Factory. setHessian2Reply (false );
???????????????????? IUserService? Basic? =? (IUserService )? Factory. create (IUserService. class ,? Url ,? GetClassLoader ());
???????????????????? Return? Basic. register ("admin ",? "123456 ");

????????????????}? Catch? (MalformedURLException? E )? {
???????????????????? Log. e ("UserActivity ",? "OnClickRegiger ",? E );
????????????????}? Catch? (Exception? E )? {
???????????????????? Log. e ("UserActivity ",? "OnClickRegiger ",? E );
????????????????}
???????????????? Return? Null;
????????????}

???????????? @ Override
???????????? Protected? Void? OnPostExecute (POUser? Result )? {
???????????????? If? (Result ?! =? Null )? {
???????????????????? If? (Result. status? ==? (200 )? {
???????????????????????? // Save and store the database
???????????????????????? New? DbHelper (). Create (result );
????????????????????}
???????????????????? Toast. makeText (UserActivity. this ,? ""? +? Result. msg ,? Toast. LENGTH_LONG). show ();
????????????????}
????????????};

????????}. Execute ();

????}
}

?

Code Description:

1. DbHelper is provided in the source code .?

2. If the project cannot be compiled, set the project's character encoding, JDK version, and Android version .?

?

Iii. Summary

5.1 advantages

A). Fully object-oriented development

B) reduce the complexity of the project and the complexity of introducing other frameworks?

C). It is very suitable for a development server and client.

Fully utilizes the features of the framework and submits the development efficiency, which is suitable for rapid development of small and medium-sized projects .?

5.2 disadvantages

A) Pay attention to the problem of id sharing between the server and the client.

5.3 others

A). ormlite supports standard JPA mnemonic, here. In this way, you can use Hibernate on the server. You can take a look at the integration example if you have time.

B) The test shows that the entire framework is also applicable to SAE. If a person is in charge of the client and server, it would be so happy!

?

4. Download

? AndroidFramework2013-03-05.zip?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.