Android网络编程之一个Android登录系统模块的实现(客户端)
写完服务器端之后,开始完成客户端,客户端斜对比较简单,新建一个Android项目结构如下:
com.moka.activities放各种主UI操作界面,com.moka.util包中放工具类
布局文件这里省略了,非常简单,看起来像这样:
先写工具类HttpUtil,此类封装了get与post方式发出请求与接受回应的各种方法
View Code
public class HttpUtil { // 基础URL,注意此处若为模拟器实验,ip不可为localhost而需要为需要为10.0.2.2 public static final String BASE_URL="http://10.0.2.2:8080/WirelessServer/"; // 获得Get请求对象request public static HttpGet getHttpGet(String url){ HttpGet request = new HttpGet(url); return request; } // 获得Post请求对象request public static HttpPost getHttpPost(String url){ HttpPost request = new HttpPost(url); return request; } // 根据请求获得响应对象response public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{ HttpResponse response = new DefaultHttpClient().execute(request); return response; } // 根据请求获得响应对象response public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException{ HttpResponse response = new DefaultHttpClient().execute(request); return response; } // 发送Post请求,获得响应查询结果 public static String queryStringForPost(String url, String account, String password){ // 根据url获得HttpPost对象 HttpPost request = HttpUtil.getHttpPost(url); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("account", account)); params.add(new BasicNameValuePair("password", password)); String result = null; try { request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 获得响应对象 HttpResponse response = HttpUtil.getHttpResponse(request); // 判断是否请求成功 if(response.getStatusLine().getStatusCode()==200){ // 获得响应 result = EntityUtils.toString(response.getEntity()); System.out.println("HttpUtil result: " + result); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "网络异常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "网络异常!"; return result; } return null; } // 发送Get请求,获得响应查询结果 public static String queryStringForGet(String url){ // 获得HttpGet对象 HttpGet request = HttpUtil.getHttpGet(url); String result = null; try { // 获得响应对象 HttpResponse response = HttpUtil.getHttpResponse(request); // 判断是否请求成功 if(response.getStatusLine().getStatusCode()==200){ // 获得响应 result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "网络异常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "网络异常!"; return result; } return null; } }
然后写Activity:
准备组件和监听事件:
View Code
public class LoginActivity extends Activity { private Button cancelBtn, loginBtn; private EditText userEditText, pwdEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("登录系统"); setContentView(R.layout.activity_login); cancelBtn = (Button) findViewById(R.id.cancelButton); loginBtn = (Button) findViewById(R.id.loginButton); userEditText = (EditText) findViewById(R.id.userEditText); pwdEditText = (EditText) findViewById(R.id.pwdEditText); cancelBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); loginBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 检查用户是否输入用户名密码 if (validate()) { // 检查是否登陆成功 if (login()) { Toast.makeText(LoginActivity.this, "成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(LoginActivity.this, "用户名称或者密码错误,请重新输入!", Toast.LENGTH_SHORT).show(); } } } }); }
验证用户是否输入用户名和密码的方法:
View Code
// 验证用户名密码是否为空 private boolean validate() { String username = userEditText.getText().toString(); if (username.equals("")) { Toast.makeText(LoginActivity.this, "用户名称是必填项!", Toast.LENGTH_SHORT) .show(); return false; } String pwd = pwdEditText.getText().toString(); if (pwd.equals("")) { Toast.makeText(LoginActivity.this, "用户密码是必填项!", Toast.LENGTH_SHORT) .show(); return false; } return true; }
验证登陆方法:
View Code
// 登录方法 private boolean login() { // 获得用户名称 String username = userEditText.getText().toString().trim(); // 获得密码 String pwd = pwdEditText.getText().toString().trim(); // 获得登录结果 String result = query(username, pwd); if (result != null && result.equals("0")) { return false; } else { System.out.println("LoginActivity result: " + result); // 将此服务器返回的此用户信息保存起来 saveUserMsg(result); return true; } }
View Code
// 根据用户名称密码查询 private String query(String account, String password) { // 组合url String url = HttpUtil.BASE_URL + "servlet/LoginServlet"; // 查询返回结果 return HttpUtil.queryStringForPost(url, account, password); }
保存用户信息的方法:
View Code
// 将用户信息保存到配置文件 private void saveUserMsg(String msg) { // 用户编号 String id = ""; // 用户名称 String name = ""; // 获得信息数组 String[] msgs = msg.split(";"); int idx = msgs[0].indexOf("="); id = msgs[0].substring(idx + 1); idx = msgs[1].indexOf("="); name = msgs[1].substring(idx + 1); // 共享信息 SharedPreferences pre = getSharedPreferences("user_msg", MODE_WORLD_WRITEABLE); SharedPreferences.Editor editor = pre.edit(); editor.putString("id", id); editor.putString("name", name); editor.commit(); }
记得在Manifest.xml中添加网络权限。
运行效果图:
成功后用户信息文件生成位置(data/data文件夹下)与内容:
至此,android与服务器登录通信模块成功实现