volley跟ssh服务结合使用

本人以前做了几年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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值