volley跟ssh服务结合使用
来源:互联网 发布:淘宝卖家怎么加入花呗 编辑:程序博客网 时间:2024/06/07 05:12
本人以前做了几年j2ee的开发,最近突然接手一个android项目 服务端用的ssh2框架,android客户端用的volley通信交互框架,下面简单介绍下ssh2怎么给通过volley给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数组了。
0 0
- volley跟ssh服务结合使用
- Dwr结合SSH使用
- volley使用加载json跟图片
- hibernate使用(结合ssh)
- yield关键字跟递归结合使用
- ViewPager跟FragmentTabHost结合起来使用
- PullToRefreshListView跟banner的结合使用
- 使用volley并结合HttpClient实现上传文件
- volley+listView结合使用时出现的问题以及解决方案
- flexigrid结合SSH常见问题及使用笔记
- 结合SSH&Nutz框架使用定时器quartz
- 使用dropbear搭建ssh服务。
- ubantu中使用ssh服务
- HttpConnection跟KXML结合使用的注意点
- 使用maven结合ssh开发出现的错误
- 关于使用cygwin下的SSH服务
- 使用SSH连接内网服务
- Xshell使用SSH服务连接Ubuntu
- BootStrap导航的创建2
- shuoj-小6的多米诺骨牌-双向dp
- android屏幕适配
- LeetCode239 Sliding Window Maximum
- _DataStructure_C_Impl:链栈
- volley跟ssh服务结合使用
- 《深入理解Java虚拟机》读书笔记——Java内存区域与内存溢出异常
- POJ_1986_Distance Queries(LCA+tarjan)
- 灾情巡视路线模型
- Joke with permutation
- android_菜单的创建_xUtils框架使用
- FFmpeg源代码结构图 - 编码
- 连接SSO CAS的代码示例
- 记我的WiFi音箱之一个网络在线播放及本地播放音乐器(一)