volley跟ssh服务结合使用
2015-08-03 23:47
381 查看
本人以前做了几年j2ee的开发,最近突然接手一个android项目 服务端用的ssh2框架,android客户端用的volley通信交互框架,下面简单介绍下ssh2怎么给通过volley给android端提供json数据。
一、ssh2端代码
struct配置文件:
ssh2服务端代码:
二:android客户端代码
请求参数:
打印结果:
这样就获取一个json数组了。
一、ssh2端代码
struct配置文件:
<?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="struts2" extends="json-default" namespace="/"> <!-- admin --> <action name="getAllCunZhuang" class="AdminAction" method="getAllCunZhuang"></action> </package> </struts>
ssh2服务端代码:
/** * 查询所有村庄 */ public void getAllCunZhuang(){ List<Tcunzhuang> assignmentList=this.adminUserService.getAllCunZhuang(); JSONArray jsArr=new JSONArray(); JSONObject jsObj=new JSONObject(); Tcunzhuang cunzhuang;// for(int i=0;i<assignmentList.size();i++){ cunzhuang=assignmentList.get(i); jsObj.clear(); jsObj.put("id", cunzhuang.getId()); jsObj.put("name",cunzhuang.getName()); jsArr.add(jsObj); } JSArr=jsArr; this.writeJson(JSArr); }
二:android客户端代码
请求参数:
package com.gnnu.orange; public class Config { //改成自己本机IP public static final String USER_LOGIN_URL = "http://192.168.1.108:8080/GnOrangeBackground/publicJSP/user.jsp"; public static final String USER_QA_URL = "http://192.168.1.108:8080/GnOrangeBackground/publicJSP/qa.jsp"; public static final String USER_Q_URL = "http://192.168.1.108:8080/GnOrangeBackground/publicJSP/question.jsp"; public static final String USER_QIMG_URL = "http://192.168.1.108:8080/GnOrangeBackground/publicJSP/questionimg.jsp"; public static final String USER_QPATH_URL = "http://192.168.1.108:8080/GnOrangeBackground/publicJSP/imagepath.jsp"; public static final String test = "http://111.75.158.12:8081/nongShangInformation/getAllCunZhuang"; }
package com.gnnu.orange; import org.json.JSONArray; import org.json.JSONObject; import com.android.volley.Request.Method; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonArrayRequest; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.JsonRequest; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.gnnu.orange.binghai.BinghaiBuWeiActivity; import com.gnnu.orange.chonghai.ChonghaiBuWeiActivity; import com.gnnu.orange.zhuanjia.LoginActivty; import android.app.Activity; import android.app.DownloadManager.Request; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; /** * 功能选择 * * @author taCi * */ public class MainActivity extends Activity implements OnClickListener{ private ImageView binghaiIv; private ImageView chonghaiIv; private ImageView zhuanjiaIv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ll_main); initView(); } private void initView() { binghaiIv = (ImageView) findViewById(R.id.iv_main_binghai); chonghaiIv = (ImageView) findViewById(R.id.iv_main_chonghai); zhuanjiaIv = (ImageView) findViewById(R.id.iv_main_zhuanjia); binghaiIv.setOnClickListener(this); chonghaiIv.setOnClickListener(this); zhuanjiaIv.setOnClickListener(this); RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); /*JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Method.POST,Config.test, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { System.out.println(response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("", error.getMessage(), error); } }) { //注意此处override的getParams()方法,在此处设置post需要提交的参数根本不起作用 //必须象上面那样,构成JSONObject当做实参传入JsonObjectRequest对象里 //所以这个方法在此处是不需要的 // @Override // protected Map<String, String> getParams() { // Map<String, String> map = new HashMap<String, String>(); // map.put("name1", "value1"); // map.put("name2", "value2"); // return params; // } }; requestQueue.add(jsonRequest); */ JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Config.test, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { // TODO Auto-generated method stub System.out.println(response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub } }); requestQueue.add(jsonArrayRequest); } @Override public void onClick(View v) { if (v == binghaiIv) { startActivity(new Intent(getApplicationContext(), BinghaiBuWeiActivity.class)); } else if (v == chonghaiIv) { startActivity(new Intent(getApplicationContext(), ChonghaiBuWeiActivity.class)); } else if (v == zhuanjiaIv) { startActivity(new Intent(getApplicationContext(), LoginActivty.class)); } } }
打印结果:
这样就获取一个json数组了。
相关文章推荐
- shuoj-小6的多米诺骨牌-双向dp
- JSP -2
- 228 Summary Range
- iOS开发——UI进阶篇(十四)modal
- _DataStructure_C_Impl:共享栈
- rsync服务搭建
- 应用洪峰应对规范
- ROS学习--(十四)编写简单的Service和Client
- js中的内部属性与delete操作符
- [2015hdu多校联赛补题]hdu5324 Boring Class
- JetBrain WebStorm 注册码
- _DataStructure_C_Impl:顺序栈
- 1033. To Fill or Not to Fill (25)
- HDU 4975
- Android Activity和Service之间的通信
- org-reveal 排版布局
- 静态内部类(嵌套类)与非静态内部类
- BOOL 类型
- [Leetcode 47, Hard] Permutations II
- CSS3 -webkit-transform(元素变换)