------------------------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
关于android客户端和服务器交互_IT/计算机_专业资料。android和服务器交互的简单... jsp(服务器)与android(客... 2页 1下载券 android客户端提交数据到... 5...
Android客户端与服务器交互(Struts)_IT/计算机_专业资料。转帖,很好的初学资料为学习存档 不为分数android 端和 Struts2 服务器端通信,交 互信息,参数采用 JSON...
Android客户端与服务器数据交互流程_计算机软件及应用_IT/计算机_专业资料。... 然后通过后台程序如 JSP,PHP, .NET 等语言将数据库中的信息与前台页面相结合...
Android手机客户端和服务器交互开发实例_互联网_IT/计算机_专业资料。Android手机客户端和服务器交互开发实例本文介绍了如何使用 Android 搭建客户端,实现手机和服务...
拓胜技术专家教你如何深入理解Android与服务器端数据交互_计算机软件及应用_IT/...file>index.jsp</welcome-file> </welcome-file-list> </web-app> struts....
用Android搭建客户端_手机和服务器交互开发实例_计算机软件及应用_IT/计算机_专业资料。用Android搭建客户端_手机和服务器交互开发实例...
Android 与服务器端数据交互(基于 SOAP 协议整合 android+webservice) 与服务器...我们在 PC 机器 java 客户端中,需要一些库,比如 XFire,Axis2,CXF 等等来支持...
Android与服务器端数据交互_计算机软件及应用_IT/计算机_专业资料。Android与...我们在 PC 机器 java 客户端中,需要一些库,比如 XFire,Axis2,CXF 等等来支持...
浅析Java web程序之客户端和服务器端交互原理_互联网_IT/计算机_专业资料。...("text/html;charset=gb2312"); 或者在 jsp 中写法是:<%@ page contentType...
浅析Java web程序之客户端和服务器端交互原理_IT/计算机_专业资料。浅析 Java...("text/html;charset=gb2312"); 或者在 jsp 中写法是:<%@ page contentType...
热门文档