最近要将一个以SSH框架开发的web应用做成Android应用,由于本人一直做的是web开发,Android不太懂,前后台的数据交互费了好大的劲才搞明白(其实很简单的,只是自己一时糊涂了),现在和大家分享一下。
首先,你要确定你的后台没有问题(写个jsp调试一下)。
第一步,所有的配置文件,只需修改struts配置文件,将返回类型改为json。
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="strutsBean" extends="struts-default,json-default" namespace="/">
<action name="memberlogin" class="cn.ehealth.actions.UserAction" method="login">
<result name="success" type="json"><param name="root">jdata</param></result>
<result name="fail" type="json"><param name="root">jdata</param></result>
</action>
</package>
</struts>
其实这两个result合成一个更方便,稍微改下就行,笔者这里就不改了。
第二步,修改actoin类。
action类中添加变量,并设置get,set方法,get方法必须要有,不然前端获取的json值为null!!!
private Map<String, Object> jdata;
public Map<String, Object> getJdata() {
return jdata;
}
public void setJdata(Map<String, Object> jdata) {
this.jdata = jdata;
}
第三步,修改login()方法,直接放代码。
public String login() {
HttpServletRequest request = ServletActionContext.getRequest();
String mobile = request.getParameter("mobile");
String password = request.getParameter("password");
loginUser = new User(mobile, password);
if (userService.login(loginUser)) {
jdata = new HashMap<String, Object>();
jdata.put("mobile", mobile);
jdata.put("password", password);
jdata.put("success", true);
return "success";
}
jdata = new HashMap<String, Object>();
jdata.put("success", false);
return "fail";
}
到此,登录就做好了,我们现在用插件来调试一下,看看返回的json。
url:http://127.0.0.1:8080/EHealth/memberlogin
看,获取到json值了,后台没问题了,移动端的的代码如下(未调试,可能有bug,大概就这样),需要先配置下网络权限。
private void login(final String mobile, final String password) {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serverUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("mobile", mobile));
params.add(new BasicNameValuePair("password", password));
HttpResponse httpResponse = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpResponse = client.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
String entityString = EntityUtils.toString(entity);
String jsonString = entityString.substring(entityString.indexOf("{"));
Log.d("haha", "entity = " + jsonString);
JSONObject json = new JSONObject(jsonString);
if(json.getString("success").equals("true")){
startActivity(new Intent(LoginActivity.this,MainActivity.class));
}else if(json.getString("success").equals("false")){
Toast toast = Toast.makeText(LoginActivity.this, "用户名或密码错误!",
Toast.LENGTH_SHORT);
toast.show();
}
} catch (UnsupportedEncodingException e) {
Log.d("haha", "UnsupportedEncodingException");
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d("haha", "ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
Log.d("haha", "IOException");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
Log.d("haha", "IOException");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
好了,到此都完成了,哈哈哈。