Android客户端开发越来越流行,但是,Android客户端对应的服务器端采用什么框架呢?对于功能较为简单的应用,我建议,直接采用java web开发最简单的MVC框架即可(很多Android应用的服务器端开发框架,我都是采用这种);但是,对于功能较为复杂,数据库表较多,逻辑关系比较复杂的应用开发,服务器端逻辑关系自然也是一样,我们是否可以考虑采用java web开发流行的SSH2/SSM等框架呢?经过实践测试,我将Android客户端连接SSM ( Spring + SpingMVC + Mybatis)框架java web服务器端的开发模式,详细给大家细细介绍一下吧~
其中,这里需要介绍的是,java web服务器端不只是需要处理来自Android客户端的请求,而且,同时需要处理来自后台管理系统(使用jsp开发前台页面)的请求,因此,在编写请求接受和处理函数时,需要分别编写(这里,我采用的是分别编写各自的处理函数)。
1.开发Android应用客户端
整个的Android客户端(TestSSMServer)的框架结构:
![这里写图片描述](https://img-blog.csdn.net/20170427104616251?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFud2VqdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1.1 activity
MainActivity.java
package com.ju.testssmserver.activity;
import com.ju.testssmserver.R;
import com.ju.testssmserver.R.id;
import com.ju.testssmserver.R.layout;
import com.ju.testssmserver.R.menu;
import com.ju.testssmserver.model.UserInfoModel;
import com.ju.testssmserver.object.User;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView userIDTV;
private TextView userNameTV;
private TextView passwordTV;
private Button getBtn;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
userIDTV = (TextView) findViewById(R.id.userIDTV);
userNameTV = (TextView) findViewById(R.id.userNameTV);
passwordTV = (TextView) findViewById(R.id.passwordTV);
getBtn = (Button)findViewById(R.id.getBtn);
intent= this.getIntent();
getBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
initActivity();
}
});
}
private void initActivity(){
/**
* Handler
* @note 用于子线程和主线程之间的数据传递
* 解决了andr4.0以上主线程无法访问网络的问题。
*/
final Handler myhandler = new Handler() {
public void handleMessage(Message msg) {
//isNetError = msg.getData().getBoolean("isNetError");
if(msg.what==0x123)
{
userIDTV.setText(String.valueOf(msg.getData().getInt("userID")));
userNameTV.setText(msg.getData().getString("userName"));
passwordTV.setText(msg.getData().getString("password"));
}
}
};
/**
* 新建子线程
* @note 用于建立网络连接,从服务器获取IMEI对应的用户信息
* 解决了andr4.0以上主线程无法访问网络的问题。
*/
new Thread(){
public void run(){
UserInfoModel userInfoModel = new UserInfoModel();
User user = new User();
try {
user = userInfoModel.getUserInfo(2);
if(user == null)
{
myhandler.post(new Runnable(){
public void run(){
showDialog("数据库中未查找到你的相关信息!请及时注册!");
}
});
}
else
{
Bundle bundle = new Bundle();
Message message = new Message();
bundle.putInt("userID", user.getUserID());
bundle.putString("userName", user.getUserName());
bundle.putString("password", user.getPassword());
message.setData(bundle);
message.what=0x123;
myhandler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 对话框显示函数
*
* @param message是要显示的内容
*/
private void showDialog(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage(message);
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
MainActivity.this.setResult(RESULT_OK, intent);
MainActivity.this.finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
对应的layout ——activity_main.xml的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ju.testssmserver.activity.MainActivity" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="26dp"
android:layout_marginTop="50dp" >
<TableRow
android:id="@+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:gravity="center|center_vertical"
android:text="Get信息"
android:textAppearance="@style/labelText"/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:gravity="center|center_vertical"
android:text="登录信息"
android:textAppearance="@style/labelText" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:gravity="center_vertical"
android:text="用\t户\tID:"
android:textAppearance="&#