Android应用获取后台基于SSH框架开发的接口数据

来源:互联网 发布:淘宝账号查看平衡网页 编辑:程序博客网 时间:2024/05/21 08:58

最近要将一个以SSH框架开发的web应用做成Android应用,现在和大家分享一下安卓端和ssh后台接口的数据交互过程。
首先,你要确定你的后台没有问题(写个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="login" class="cn.xiaoniu.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://192.168.0.130:8080/xiaoniu/login

如果获取到json值了,就说明后台没问题了,移动端的的代码如下,需要先配置下网络权限。

 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));        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("{"));             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();         }    }

好了,到此都完成了,其他请求同理。有问题可以提问交流

原创粉丝点击