文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > jsp(服务器)与android(客户端)交互
侵权投诉

jsp(服务器)与android(客户端)交互

------------------------jsp(服务器)与android(客户端)交互

使用两个Eclipse ,一个做客户端,一个服务器

步骤:
在服务器端:
1.创建web工程
2.添加hibernate
3.添加表(登陆,查询部门)
4.写service

//登陆
public boolean checkLogin(String username, String password) {
Users instance = new Users();
instance.setUsername(username);
instance.setPassword(password);
List list = usersDAO.findByExample(instance);
if (list != null && list.size() > 0)
return true;
return false;
}


//查询部门
public List getAllDep() {
return depDAO.findAll();
}
//查询
public String getAllDepToJson() {
List<Dep> list = getAllDep();
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"emps"});
String json = JSONArray.fromObject(list,config).toString();---------------要使用json与android交互
return json;
}

5.写servlet(emp.do)标签(method=login)

dologin方法

String username = request.getParameter("username");
String password = request.getParameter("password");
boolean f = empService.checkLogin(username, password);
if (f)
{
String json = empService.getAllDepToJson();
out.write(json);
}
else
out.write("0");
}

客户端:
1.写好登陆界面
2.在activity里:
申明一个客户端
private HttpClient client = new DefaultHttpClient();

点击登陆事件
public void onClick(View v) {
EditText txtUsername = (EditText) findViewById(R.id.txtUsername);
EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
String username = txtUsername.getText().toString();
String password = txtPassword.getText().toString();
String result = login(username, password);
// System.out.println(result);
//第一个参数这个Activity
Toast.makeText(S11_android_5Activity.this, result, 1).show();------------弹出toast,
}


login方法
try {
HttpPost httpPost = new HttpPost(
"doc.guandang.net;
+ Math.random()); ---------------连接数据库
NameValuePair usernameValue = new BasicNameValuePair("username",
username);
NameValuePair passwordValue = new BasicNameValuePair("password",
password);
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(usernameValue);
list.add(passwordValue);
HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");
httpPost.setEntity(entity);

HttpResponse response = client.execute(httpPost); -----------------到服务器
String result = EntityUtils.toString(response.getEntity(), "utf-8"); --------服务器返回结果
return result;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.ge

第1页

你可能喜欢

  • 数据服务
  • Android开发
  • 服务客户
  • web服务器
  • Android网络通信
  • mysql数据库
  • 数据库实例

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email