您的位置:首页 > 其它

volley跟ssh服务结合使用

2015-08-03 23:47 381 查看
本人以前做了几年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数组了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: