内容:
1、服务器在本地搭建,利用Tomcat运行servlet容器
2、继承于httpServlet类的自定义实现与客户端的交互,结合MysqL,jdbc知识
3、客户端实现简单的注册和登录验证
注意点:
1、web.xml里入口定义要正确,否则登录客户端连接不到
2、当在Tomcat中运行时,需要在WEB_INF文件夹中手动添加jdbc库文件,偶尔会在运行过后消失,要注意。(或者直接脱离Eclipse就不会有这个问题)
3、sql语句要多测试,否则容易写错但执行不会报错。
4、注意事先需启动MysqL。
package com.example.java4androidmysql;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String URL = "http://192.168.2.1:8080/JavaRegisterMysql/login";
public static String res="";
public static String name = "";
public static String code = "";
public static String phone =" ";
public static String chose="0";
Button connectButton=null;
Button registerButton=null;
EditText noteUser=null;
EditText notePassword=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
connectButton=(Button)findViewById(R.id.btnConnect);
registerButton=(Button)findViewById(R.id.btnRegister);
noteUser=(EditText)findViewById(R.id.editUserName);
notePassword=(EditText)findViewById(R.id.editPassword);
//注意类Class DriverManager Statement ResultSet
connectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "正在登录", Toast.LENGTH_SHORT).show();
name = noteUser.getText().toString().trim();
code = notePassword.getText().toString().trim();
chose="1";
new SubmitAsyncTask().execute(URL);
Toast.makeText(MainActivity.this, "res = "+res, Toast.LENGTH_SHORT).show();
if(res.equals("2")){
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//Intent intent=new Intent();
//intent.setClass(MainActivity.this, WriteNote.class);
//MainActivity.this.startActivity(intent);
}else if(res.equals("1")){
Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}else if(res.equals("3")){
Toast.makeText(MainActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}else if(res.equals("4")){
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
}else if(res.equals("-1")){
Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
}
}
});